I have a form in which the user can optionally select an option and if the user does not choose an option I need the default value of 'null' be submitted to database. I am using laravel 5.1 so my input field is as follows:
<div class="form-group col-lg-4">
{!! Form::label('cat1', 'CAT Tool 1', ['class' => 'control-label']) !!}
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-cog"></span>
</div>
{!! Form::select('cat1', $cats , null , ['class' => 'form-control']) !!}
</div>
</div>
Here the $cats
array is an array of CAT Tools ($cats=['SDL Trados', 'Deja Vu', 'WordFast', 'OmegaT', 'Fluency'];
). I have similar fields in which an array of database attributes are returned (e.g. In the controller create method I have $languages=Language::lists('fa_name', 'id');
). How can I accomplish this?
The lists
method used to return an array but as of Laravel 5.1 it returns a collection so I needed to convert the $languages to an array like this:
$languages=Language::lists('fa_name', 'id')->all();
or
$languages=Language::lists('fa_name', 'id')->toArray();
Next in the view I can use the following:
{!! Form::select('from1', [null => 'Select your language'] + $languages, null , ['class' => 'form-control']) !!}
which will add an empty option to the select field.