katie hudson
katie hudson

Reputation: 2893

Laravel 5 get select option value

in my create function, I do

$clients = Client::lists('clientName');
return View::make('projects.create', compact('clients'));

In my View, I then populate the select by doing this

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

When I view the html, I see something like the following

<select name="clientName" id="clientName" class="form-control">
    <option value="0">John Dandy</option>
</select>

Now in my store function, I need to get the value John Dandy, not 0. If I do

$clientName = Input::get('clientName');

I get 0. How can I set the value to the clientName or get the option in my store function?

Thanks

Upvotes: 0

Views: 1180

Answers (1)

Ahmad Baktash Hayeri
Ahmad Baktash Hayeri

Reputation: 5890

If you specify only one argument for the lists method, it will return an array of values automatically mapped to a counter key part starting with 0 (that's why you're getting that 0).

However, using Laravel's Query builder method lists, you can specify a custom key column as the second parameter (Documentation).

So if you were to get a key value pair where the value acts as the key itself, you could do the following:

$clients = Client::lists('name', 'name');

Or, if you wanted an id => client array:

$clients = Client::lists('name', 'id');

Upvotes: 2

Related Questions