您好,我正在用我的数据库中存在的客户端填充一个选择下拉列表,但是我首先想要一个占位符,例如请选择一个客户端
。有谁知道语法?
这是我目前所拥有的:
@if(count($client_options)>0)
{{ Form::select('client', $client_options , Input::old('client'), array('placeholder' => 'Please select a client', 'id' => 'select_client', 'class' => 'chosen-select select', 'tabindex' => '2', )) }}
@endif
占位符属性不起作用,有人知道怎么做吗?提前致谢
最佳答案
假设您正在 Laravel 表单构建器中创建一个下拉列表。那么代码应该是这样的——
在 Controller 中 -
$categories = Category::select('id', 'name')->lists('name', 'id')->prepend('Select a category', '')->toArray();
在 View 中 -
{!! Form::select('cat_id', $categories, old('cat_id')) !!}
使用 Laravel 5.x 测试。
或者如果你有一个像这样的数组 -
$array = ['1' => 'lorem ipsum', '4' => 'Another text'];
并且在将这个数组传递给 View 之后 -
{!! Form::select('cat_id', $array, old('cat_id')) !!}
不会有占位符。如果你传递下面的数组 -
$array = ['' => 'Select category', '1' => 'lorem ipsum category', '4' => 'Another category'];
或者有一个你想传递给 View 的集合,然后构建选择/下拉列表
$array = $collection->prepend('Select a category', '')->toArray();
您需要传递一个数组才能构建下拉列表。
Note:
array_unshift
orarray_merge
will not work as expected!
关于php - Laravel 数据库选择带占位符的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25222924/