如何将数据属性添加到laravel {{Form::select}}
数组中?
例如,我希望每个选项都像这样
<option value="1" data-days="182">6 Months</option>
<option value="2" data-days="365">1 Year</option>
如何在以下选择数组中添加数据属性?
天数表
id | days | name
1 | 182 | 6 Months
2 | 365 | 1 Year
查看
{{Form::select('amount_of_days', $days, null, ['placeholder' => 'Amount of days'])}}
答案 0 :(得分:0)
为此要求,您需要使用循环创建<option>
并简单地创建html
<select name="amount_of_days" placeholder="Amount of days">
@foreach($days as $val)
<option value="{{ $val['id'] }}" data-days="{{ $val['days'] }}"> {{ $val['name'] }}<option>
@endforeach
</select>
答案 1 :(得分:0)
将自定义值设置为form :: select的最佳方法是使用form :: select函数的属性参数。
controller.php
$contact_options = Contact::all();
$contact_attributes = $contact_options->mapWithKeys(function ($item) {
return [$item->id => ['data-otherInfo' => $item->otherInfo]];
})->toArray();
$contact_options = $contact_options->pluck('name', 'id')->toArray();
blade.php
{!! Form::select('contact_id', $contact_options, old('contact_id', null), ['class'=>"form-control", 'id'=>"contact_id", 'required' => 'required'], $contact_attributes) !!}