php - 在 laravel 表单中使用数据属性选择
问题描述
如何将数据属性添加到 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'])}}
解决方案
将自定义值设置为 form::select 的最佳方法是使用 form::select 函数的属性参数。
控制器.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();
刀片.php
{!! Form::select('contact_id', $contact_options, old('contact_id', null), ['class'=>"form-control", 'id'=>"contact_id", 'required' => 'required'], $contact_attributes) !!}
推荐阅读
- javascript - 如何让 React 状态数组永久更新
- python - Python 最小缺失整数:Codility 演示任务。我错过了什么?
- java - android设备睡眠模式下的耳机按钮点击计数
- java - 如何在 JPA 中将标准构建器与数组列表一起应用?
- flutter - 颤振。构建 Gradle 找不到参数的方法 signingConfigs() [build_al27st8bbmybmo7hiqzirqbvw$_run_closure2@2176e371]
- xml - 如何使用站点地图包在下一个 js 中生成动态站点地图?
- firebase - 在flutter中获取firestore快照的长度返回“未来实例
" - python - Python可以打开浏览器...可以打开网站吗?
- api - 将前端 Google API 移至后端
- google-cloud-platform - GCP CloudBuild 日志