php - Laravel 数据库选择带占位符的下拉列表

标签 php select laravel placeholder

您好,我正在用我的数据库中存在的客户端填充一个选择下拉列表,但是我首先想要一个占位符,例如请选择一个客户端。有谁知道语法?

这是我目前所拥有的:

@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 or array_merge will not work as expected!

关于php - Laravel 数据库选择带占位符的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25222924/

相关文章:

sql - 在 Postgres 中选择每个用户的每 N 行

select - Grails,<g:select>中的可编辑值

sql - 为什么 SELECT DISTINCT 返回两次相同的值?

php - required_with 和 required_with_all laravel 验证之间有什么区别

php - Mysql从查询结果中查询

PHP 显示汉字 : SET NAMES 'utf8' not working

php - 如何将Blade中的 bool 值传递给Vue组件Laravel 7?

php - Laravel Blade "old input or default variable"?

php - AWS S3 CopyObject 版本

php - 拆分 PHP 生成器