Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add item to select box with an Eloquent collection

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');
like image 557
Dwight Avatar asked Jul 12 '13 01:07

Dwight


2 Answers

You could also do

$campuses = array('' => 'Select...') + Campus::lists('name', 'id');

This is the way I use it, sum 2 arrays

like image 108
Israel Ortuño Avatar answered Oct 29 '22 17:10

Israel Ortuño


You can merge 2 arrays with array_merge function.

So, the answer will be

$campuses = array_merge(array('Select...'), Campus::lists('name', 'id'));

like image 24
Umut Sirin Avatar answered Oct 29 '22 19:10

Umut Sirin