Gammer
Gammer

Reputation: 5628

Laravel: Select2 option value

I have the following code in my view:

{!! Form::select(   'projectSkills', 
                    ['' => ''] + \App\Skill::where('type','freelancer')
                                ->pluck('name', 'id')
                                ->toArray(), 
                    @$skills, 
                    [
                      'class' => 'select2',
                      'multiple'=>'multiple'
                    ]
                  ) !!}

Where the value of the options comes as 0,1,2,3,4,5,

I want the value as a name which is fetching from the table.

What am i doing wrong there ?

Upvotes: 3

Views: 1018

Answers (1)

Tschallacka
Tschallacka

Reputation: 28742

You are making a lists consisting of: [id => name] by calling pluck('name','id')

This will be fed into the form causing a dropdown list:

<option value="id">name</option>

If you wish to change the 'value' part, you need to supply an array consisting of the correct value part.

You could do pluck('name','name'); to get the values you want into the form.

If you look at the the documentation of the method pluck you see that the second parameter is for the 'key' part of the array that will be returned.

Upvotes: 2

Related Questions