Laravel: how to populate a blade SELECT with values from a where statement

38,076

Solution 1

I found an answer that worked for me:

Use fluent instead of eloquent, which will look something like this:

$client = DB::table('clients')->where('group_id', 1)->lists('name');
return View::make('index', compact('client'));

Then in your view just call it inside blade form tags like this:

{{ Form::select('client_id', $client, Input::old('client_id')) }}

@KyleK, thanks for trying to help.

Solution 2

Controller:

$client = Client::where('group_id', 1)->pluck('name', 'id');

View:

{!! Form::select('client_id', $client, Input::old('client_id'), ['class'=> 'form-control'])  !!}

Result:

<select id="client_id" class="form-control" name="client_id">
  <option value="1">John</option>
  <option value="2">Karen</option>
</select>

Solution 3

The lists() must be called at last

$client = Client::where('group_id','=', 1)->lists('name','id');

Solution 4

This would work too

Client::where('group_id','=', 1)->lists('name');

Solution 5

Not sure if this is a typo or not, but you're not retrieiving properly,

This line should be...

    $client = Client::lists('name', 'id')->where('group_id','=', 1)->get();

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.

Share:
38,076

Related videos on Youtube

Josh
Author by

Josh

Updated on November 01, 2020

Comments

  • Josh
    Josh over 3 years

    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?

  • Josh
    Josh over 10 years
    Hi, thanks for the answer - I'm getting an error with this "Call to a member function where() on a non-object" do you know why that would be? It's signalling out the line you proposed above.
  • Carlo
    Carlo over 9 years
    Isn't a bad practice to call directly a table by name?
  • Ketan Akbari
    Ketan Akbari almost 8 years
    @kylek if i have array of id than its not working. ('group_id','=', $ids) here, $id contains[1,2,3,]. what to do?
  • shakee93
    shakee93 over 7 years
    lists is deprecated now use pluck
  • jonlink
    jonlink over 6 years
    Yeah, should probably be using the model here.
  • jonlink
    jonlink over 6 years
    Also lists() is now deprecated. Adrian's answer is better.
  • jonlink
    jonlink over 6 years
    lists() is now deprecated. Adrian's answer is better.