我正在尝试使用blade
创建一个下拉列表
我已经检查了这个问题Laravel 4刀片下拉列表类属性
我试过了:
{{Form::select('category_id', $categories)}}
,结果是:
<select name="category_id"><option value="0">{"id":2,"name":"Apartment"}</option><option value="1">{"id":3,"name":"Car"}</option></select>
我不知道如何显示选项的name
值。加上我不知道如何将每个选项的值设置为其id
我知道第四个参数是选项1,我尝试这样做
{{Form::select('category_id', $categories, '', $categories)}}
但是我得到了这个异常:
htmlentities() expects parameter 1 to be string, array given (View:
请注意,$categories
是数组,每一行都有id
和 name
更新1
我像这样将值从控制器发送到视图
$categories = Category::All(['id', 'name']);
其中Category
为模型
Form::select()
需要像
而$categories = Category::all()
给出的多维数组看起来像
也就是说只要改变
$categories = Category::all(['id', 'name']);
$categories = Category::lists('name', 'id');
那么这个就可以了
{{ Form::select('category_id', $categories) }}