简体   繁体   English

Laravel:Select2选项值

[英]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 , 选项的价值为0,1,2,3,4,5

I want the value as a name which is fetching from the table. 我想将值作为从表中获取的name

What am i doing wrong there ? 我在那里做错了什么?

You are making a lists consisting of: [id => name] by calling pluck('name','id') 您正在通过调用pluck('name','id')创建一个包含以下内容的列表: [id => name]

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'); 你可以做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. 如果查看方法的文档,您会看到第二个参数是针对将返回的数组的“key”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM