我正在尝试使用blade
创建一个下拉列表
我已经检查过这个问题Laravel 4 blade drop-down list class attribute
我尝试了这个:
{{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
我知道第四个参数是选项一,而我尝试这样做
{{Form::select('category_id', $categories, '', $categories)}}
但是我有一个异常(exception):
htmlentities() expects parameter 1 to be string, array given (View:
请注意
$categories
是数组,每行都有id
和 name
更新1
我从 Controller 发送值以这种方式查看
$categories = Category::All(['id', 'name']);
其中
Category
是模型
最佳答案
Form::select()
需要像这样的平面数组
array( 1 => 'Appartment', 2 => 'Car' )
whereas $categories = Category::all()
gives you a multidimensional array that looks like
array( 0 => array( 'id' => 1, 'name' => 'Appartment' ), 1 => array( 'id' => 2, 'name' => 'Car' ) )
That being said just change
$categories = Category::all(['id', 'name']);
至
$categories = Category::lists('name', 'id');
这样就可以了
{{ Form::select('category_id', $categories) }}
关于php - laravel Blade 如何设置下拉列表的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24708260/