I have a select box on a form which uses data that is listed from an Eloquent model (Laravel 4):
$campuses = Campus::lists('name', 'id');
And the form:
{{ Form::select('campus_id', $campuses) }}
However, I would like to have the first option on the form be Select...
so that when the user has not selected an option yet, the first option does not become the default.
How can I prepend another option to the beginning of the Eloquent collection?
I've tried something like:
$campuses = array('Select...') . Campus::lists('name', 'id');
You could also do
$campuses = array('' => 'Select...') + Campus::lists('name', 'id');
This is the way I use it, sum 2 arrays
You can merge 2 arrays with array_merge
function.
So, the answer will be
$campuses = array_merge(array('Select...'), Campus::lists('name', 'id'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With