更改Laravel 5.4中的起始选择值
I'm using Laravel 5.4 and i'm trying to make select form with this code:
{{ Form::select('country', App\Countries::all()->pluck('en_title'), old('country', $user->country), array('class' => 'form-control') ) }}
The result that i'm receiving is this:
<select name="country" class="form-control">
<option value="0">a</option>
<option value="1">b</option>
<option value="2">c</option>
<option value="3">d</option>
...
</select>
The problem is that i don't have in the database value 0, so i want to start from value="1" like:
<select name="country" class="form-control">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
<option value="4">d</option>
...
</select>
What should i change? I read everything possible, but no one try to do this with the specific function, or this is impossible with this function?
我正在使用Laravel 5.4,我正在尝试使用此代码制作选择表单: p> \ n
{{Form :: select('country',App \ _Names :: all() - &gt; pluck('en_title'),old('country',$ user-&gt; country ),array('class'=&gt;'form-control'))}}
code> pre>
我收到的结果是: p>
&lt; select name =“country”class =“form-control”&gt;
&lt; option value =“0”&gt; a&lt; / option&gt;
&lt;选项值 =“1”&gt; b&lt; / option&gt;
&lt; option value =“2”&gt; c&lt; / option&gt;
&lt; option value =“3”&gt; d&lt; / option&gt;
...
&lt; / select&gt;
code> pre>
问题是我在数据库中没有值0,所以我想从value =“1”开始像 : p>
&lt; select name =“country”class =“form-control”&gt;
&lt; option value =“1”&gt; a&lt; / option&gt; \ n&lt; option value =“2”&gt; b&lt; / option&gt;
&lt; option value =“3”&gt; c&lt; / option&gt;
&lt; option value =“4”&gt; d&lt; / opt 离子&gt;
...
&lt; / select&gt;
code> pre>
我应该更改什么? 我阅读了所有可能的内容,但没有人尝试使用特定功能执行此操作,或者使用此功能无法做到这一点? p>
div>
You can specify the key when you pluck. In your case it'd be
{{ Form::select('country', App\Countries::all()->pluck('en_title', 'id'), old('country', $user->country), array('class' => 'form-control') ) }}
Which would result in an array such as
[1 => 'United Kingdom', 2 => 'United States']
Where the key is the ID of the country.
In addition to the above, you're selecting all then plucking the data you want from the resulting data set. You can use pluck
in place of all
which builds a select for you and just grabs the relevant fields. For example
App\Countries::pluck('en_title', 'id')
Also, on a side note you should probably create that list of countries in your controller rather than calling your database in your views. It'd simplify the code in your view as well as generally being better practice
// Controller
$countries = \App\Countries::pluck('en_title', 'id');
return view('viewname')->withCountries($countries);
// View
{{ Form::select('country', $countries, old('country', $user->country), ['class' => 'form-control'] ) }}