I understand you can send values to a select statement like this:
Controller:
$client = Client::lists('name', 'id');
return View::make('index', compact('client'));
And populate this in my view like so:
View:
{{ Form::select('client_id', $client, Input::old('client_id')) }}
But how do I populate only records from Clients where group_id = 1 for example.
I tried:
$client = Client::lists('name', 'id')->where('group_id', 1)->get();
and
$client = Client::lists('name', 'id')->where('group_id','=', 1)->get();
But it doesn't seem to work like that and gives me the error "Call to a member function where() on a non-object"
Any ideas on how to make it work?
The lists() must be called at last
Not sure if this is a typo or not, but you're not retrieiving properly,
This line should be...
Also....
Sometimes when populating lists, you will get models, and its easy to pass them, but sometimes u get arrays (fluent, raw etc) , and in those cases you have to access manually, and build the form with HTML because you have to access it differently.
I found an answer that worked for me:
Use fluent instead of eloquent, which will look something like this:
Then in your view just call it inside blade form tags like this:
@KyleK, thanks for trying to help.
Controller:
View:
Result:
This would work too
If you prefer to use Laravel's query builder (e.g. for performance reasons):
or, with a
select
to pull only what's needed:In your view: