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?
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'] ) }}