如何通过 laravel 的 lists() 传递不止 id 和 name

How to pass more than id and name through laravel's lists()

我需要将超过 "name" 和 "id" 传递到 select 框。 我的模型有 ID、名称和价格。

所以我将它传递给视图:

$parts = Part::all()->lists('name','id);

我想要 select 框选项,例如:

<option value='id' data-price='price'>name</option>

我的猜测是尝试将数组作为 lists() 方法中的第一个参数传递,但我不知道是否可以使用 Form helper。

 $parts = Part::all()->lists('["name"=>name, "price"=>price]','id');

有什么建议吗?

lists() 不是用于构建 select,它只是从集合中创建一个数组。您必须将完整模型传递给视图,然后手动构建 select:

<select name="part">
    @foreach($parts as $part)
        <option value="{{ $part->id }}" data-price="{{ $part->price }}">
            {{ $part->name }}
        </option>
    @endforeach
</select>

尝试做类似

的事情
$parts = Part::all()->get(array('id', 'name', 'price'))->toArray();

这应该只为您提供关联数组中需要的列:)