【问题标题】:how to set values from table in laravel blade如何在 laravel 刀片中从表中设置值
【发布时间】:2018-06-22 10:43:36
【问题描述】:

我正在努力在 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]);

dropdown

【问题讨论】:

  • 您的意思是下拉列表的第一部分需要来自 CSV,第二部分来自货币?
  • 感谢您的回复,并非完全在下拉列表中,所有数据(货币列表)都将从 TABLE 值显示,但第一个值将显示为 CSV 文件中的设置。
  • 例如。在 csv 文件中以货币 = USD 然后在下拉菜单中 = USD 值将首先显示,然后是所有其他货币值。
  • 美元的价值也在表中?还是 table 和 csv 都有不同的值?
  • 是美元也在表格中,没有表格和csv具有相同的值。

标签: laravel laravel-blade laravel-views


【解决方案1】:

更新

按 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

【讨论】:

  • 记录来自table,csv文件中设置的货币会与table记录匹配并设置为first
  • 是的,我明白了,$csvFiled 在这里设置您的 csv 字段,然后运行代码,否则将 $csvFiled 设置为静态假设为“美元”,然后打印 $currencies 的结果
  • {{ $selected_currency = \App\Models\Currency::orderByRaw("FIELD('code' , $array['currency']) DESC")->pluck('code', ' id')}}
  • 回显 $array['currency'] 的值并粘贴到评论中或添加 $csvFiled = $array['currency'];休息是一样的
  • 哦,对不起,我弄错了,我传递的是字符串 $array['currency'] 这就是为什么它给了我一个错误
猜你喜欢
  • 2016-10-28
  • 2017-05-07
  • 2021-10-03
  • 2015-08-06
  • 1970-01-01
  • 1970-01-01
  • 2020-10-25
  • 2019-09-11
  • 2020-03-14
相关资源
最近更新 更多