从表单中的选择列表中发送值而不是键

问题描述 投票:0回答:1

下面是从字段中删除重复值并仅返回一个值的代码。

Controller中的数组值

$job_types = DB::table('jobs')->pluck('job_type')
            ->unique(function ($item){
                return $item;
            });

下面使用字段中提取的数组创建一个选择列表。

HTML

<div class="form-group col-sm-4" id="job_type">
    {!! Form::label('job_type', 'Emne:') !!}

    {!! Form::select('job_type', $job_types, null, ['class' => 'form-control', 'id' => 'job_type']) !!}
</div>

enter image description here

上面的图像是我在表单中的选择列表。我尝试在Laravel中的表单中发送选定的值而不是键。

但是,当我在选择列表中选择一个值时,他们总是返回一个键而不是一个值,如下面的图像(数据库屏幕截图)。哪部分代码错了?

enter image description here

'job_types'集合如下。

enter image description here

javascript html laravel collections
1个回答
0
投票

作为一个$job_types,它是id设置$job_types作为它的真名。

所以你必须重建:

return $item->name; // name is the proper value from your database

$job_types = DB::table('jobs')->pluck('job_type')
            ->unique(function ($item){
                return $item->name;
            });

确保你使用正确的方法:https://laravel.com/docs/5.6/collections#method-pluck

© www.soinside.com 2019 - 2024. All rights reserved.