Laravel刀片如何设置选项下拉列表


laravel blade how to set the options for drop down list

我正在尝试使用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()需要像

这样的平面数组<>之前阵列(1 =>"公寓",2 =>"车")之前

$categories = Category::all()给出的多维数组看起来像

<>之前阵列(0 => array('id' => 1;'name' => ' apartment '),1 => array()'id' => 2;'name' => 'Car'))之前

也就是说只要改变

$categories = Category::all(['id', 'name']);

$categories = Category::lists('name', 'id');

那么这个就可以了

{{ Form::select('category_id', $categories) }}