php 为 Laravel 表单中的选择列表添加默认值::选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27892437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add default value to select list in Laravel form::select
提问by user3189734
Simple question, I hope.
简单的问题,我希望。
I need to add a default value to my select list 'Please Select', and set it to disabled.
我需要在我的选择列表“请选择”中添加一个默认值,并将其设置为禁用。
<select name="myselect" id="myselect">
<option value="" disabled>Please Select</option>
<option value="1">Item 1</option>
<option value="2">Item 2</option>
</select>
My current laravel form::select is
我目前的 Laravel 表单::选择是
{{
Form::select(
'myselect',
$categories,
$myselectedcategories,
array(
'class' => 'form-control',
'id' => 'myselect'
)
}}
How can I amend this to include the default option value?
如何修改它以包含默认选项值?
回答by nterms
In Laravel 5.1 you can prepend the default item if the list is a collection (result of a Eloquent::lists()
)
在Laravel 5.1,你可以预先设置默认的项目,如果该列表是一个集合(的结果Eloquent::lists()
)
$categories = Category::lists('name', 'id');
$categories->prepend('None');
回答by lukasgeiter
You can use array_merge
like this:
你可以这样使用array_merge
:
{{
Form::select(
'myselect',
array_merge(['' => 'Please Select'], $categories),
$myselectedcategories,
array(
'class' => 'form-control',
'id' => 'myselect'
))
}}
Alternatively you can set the placeholder somewhere before the select:
或者,您可以在选择之前的某处设置占位符:
$categories[''] = 'Please Select';
Update
更新
To add the disabled attribute you can try this: (untested)
要添加禁用属性,您可以尝试以下操作:(未经测试)
{{
Form::select(
'myselect',
array_merge(['' => ['label' => 'Please Select', 'disabled' => true], $categories),
$myselectedcategories,
array(
'class' => 'form-control',
'id' => 'myselect'
))
}}
回答by CCJ
Add 'placeholder' => 'Please Select'
into the Form::select
.
添加'placeholder' => 'Please Select'
到Form::select
.
{!!
Form::select(
'myselect',
$categories,
null,
['class' => 'form-control', 'placeholder' => 'Please Select'])
!!}
回答by ajlaracast
Or just put placeholder, for example:
或者只是放置占位符,例如:
[
'class' => 'form-control',
'id' => 'myselect',
'placeholder' => 'None'
]
That will do the trick.
这样就行了。
回答by Pranay Aryal
$categories = Category::lists('name', 'id');
$categories->prepend('None', 0);
回答by Md Rashedul Hoque Bhuiyan
For, prepending Please Selectwith empty value
For, prepending Please Selectwith empty value
$categories = Category::lists('name', 'id');
$categories->prepend('Please Select', '');
This code will populate something like this ,
这段代码将填充这样的东西,
$categories[''] = 'Please Select';
$categories[0] = 'item 1',
$categories[1] = 'item 2';
Now you can use something like this:
现在你可以使用这样的东西:
{!! Form::select('myselect', $categories, '',['id'=>'myselect']) !!}
This is helpful for form validation also, like required.
这也有助于表单验证,例如required。
回答by Kaushik shrimali
Now, Default India was select
现在,默认印度被选中
{{ Form::select('terms_agreement_type', [null => 'Select Control'] + ['India' => 'India', 'Nepal' => 'Nepal'], 'India') }}
{{ Form::select('terms_agreement_type', [null => 'Select Control'] + ['India' => 'India', 'Nepal' => 'Nepal'], 'India') }}
回答by rickslayer
You can use pluck
你可以使用采摘
Controller
控制器
$role = Model::all();
{{ Form::select('nameselect', $role->pluck('name', 'id')->prepend('default value...', ""))}}
回答by Jerson Moreno
i put my solution for this post. I hope can help to somebody
我把我的解决方案放在这篇文章中。我希望可以帮助某人
i use a php function to add an option to the array of the model
我使用 php 函数向模型数组添加一个选项
array_unshift($model, ['value' => '', 'name' => 'Select value']);
array_unshift($model, ['value' => '', 'name' => 'Select value']);
回答by Shuhad zaman
i have used placeholder and it worked for me
我使用了占位符,它对我有用
{!! Form::select('supplier', $suppliers, null, ['class' => 'form-control', 'placeholder' => 'Please Select']) !!}
{!!Form::select('supplier', $suppliers, null, ['class' => 'form-control', 'placeholder' => '请选择']) !!}