首页 > 解决方案 > 如何在 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]);

落下

标签: laravellaravel-bladelaravel-views

解决方案


更新

按 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表单选择


推荐阅读