【问题标题】:laravelcollective: add extra attribute to each options(Form::Select)laravelcollective:为每个选项添加额外的属性(Form::Select)
【发布时间】:2020-11-16 19:10:21
【问题描述】:
<select class="form-control" id="user" name="car">
  <option value="0" selected="selected">Select Cars</option>
  <option dta-img='1.jpg' value="134">Volvo</option>
  <option dta-img='2.jpg' value="135">Noah</option>
  <option dta-img='3.jpg' value="136">Marcidis</option>
  <option dta-img='4.jpg' value="143">London Express</option>
</select>

我的选项数组:

public function guardianList()
    {
      $list = ["Select Cars"];
        foreach ($carss->all() as $car) {
            $list[$car->id] =  $car->name;
        }
        return $list;
}

   Form::select('car',$list, null, null);

如何为所有选项添加额外的动态 html 属性? 我想在 jquery select2 中的每个选项前面显示图像。为此,我需要从 Form::Select() 发送图像。我有什么简单的方法可以在不使用 marcos 的情况下做到这一点?

【问题讨论】:

    标签: php laravel-5


    【解决方案1】:

    解决了这个问题: 在第四个参数中传递数组。

    $opt_attributes = collect($guardians->all())
            ->mapWithKeys(function ($item) {
                return [$item->id => ['data-img' => $item->photos->last()->name]];
            })->all();
    

    创建一个数组链接并将其作为第四个参数传递。

    【讨论】:

      【解决方案2】:

      我不知道是从哪个版本开始的,但是 laravelcollective/html 5.8 版本将 optionParameters 作为第五个参数,而不是第四个

      【讨论】:

        猜你喜欢
        • 2015-10-19
        • 2015-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-13
        相关资源
        最近更新 更多