laravel - 如何在 laravel 刀片中从表中设置值
问题描述
我正在努力在 laravel Blade 的下拉列表中显示表中的货币列表。在下拉选项中,我想设置 csv 文件中设置的第一个值货币。但下拉记录来自表。
如何在上传的 csv 文件中设置下拉列表中的第一个值。
谢谢你的帮助。
以下是我的代码。
我的.blade.php
将来自 csv 的货币存储在variable = $array['currency']
<td class="currency"style="width: 10%;">
{{ Form::select('currency',$currencies , "",['class'=>"form-control select-currency",'data-plugin'=>"select2",
]) }}
</td>
MyModel.php
$currencies = Currency::all()->pluck('code', 'id');
$questions_table = \View::make('confirmation.form_questionnaire',
['questions' => $record['questions'], 'response_type' =>$record['response_type'], 'currencies' => $currencies]);
解决方案
更新
按 CSV 提交的货币代码订购
$csvFiled = 'USD';
$currencies = Currency::orderByRaw("FIELD(code , '$csvFiled') DESC")->pluck('code', 'id');
或者
首先获取数组中的列表
$currencies = Currency::pluck('code', 'id')->toArray();
然后找到CSV记录并设置为第一个索引
$csvValuekey = array_search('USD', $currencies); // $key = 2;
unset($users[$csvValuekey]); // Remove from main array
$fOption = [$csvValuekey => 'USD'];
$finallList = array_merge($fOption,$currencies); // Merge and set on top
现在添加$finallList
表单选择
推荐阅读
- php - 在单个 Elastic Beanstalk 环境中运行多个 PHP 网站
- r - 从 ggplots 列表中设置常见的 y 轴范围
- spring - 在 spring boot 2.3.1.RELEASE 中是否禁用了 data.sql?
- selenium - Jsoup 从一开始只获取几行 HTML,甚至不到 25%
- python - 如何让所有的海龟同时移动
- javascript - 如何在角度 ag 网格中显示子数组
- javascript - 如何在不使用开关的情况下返回值,如果在 javascript 中使用 else
- mysql - 如何避免 Mariadb Galera 中的 SST
- angular - 角度全局函数在悬停时返回 elementId
- javascript - 响应 css 更新样式 onclick