In your query you can try and use:
->lists('name', 'id');
http://laravel.com/docs/queries#selects -> Retrieving A List Of Column Values
The query will build it with 'id' => 'name' and thus will build your list as you want.
Then you can use the form helper and pass it the array/object:
{{ Form::select('city_bldg_id', $sources, null, ['id' => 'city_bldg_id', 'class' => 'form-control']) }}
P.S. ++ New Mexico~~
Edit: Updated to match your code.
OK, thanks! I passed the list this way (based on another hint I found elsewhere):
{{ Form::select('city_bldg_id', $cities, Input::old('city_bldg_id')) }}
And it worked!
Buy now I have 2 questions:
Gracias! (p.s. where in NM? :)
http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html#365-403
Also, I am from Gallup.
Thanks again -- Answers:
When I try to put in the default value, I get an error. e.g.,
{{ Form::select('city_mail_id', $cities, '2' }}
gets: "syntax error, unexpected ';'", whether or not I put the single quotes around the number.
I love Gallup :) I'm in ABQ.
{{ Form::select('city_mail_id', $cities, '2') }} //You forgot to close the method.
As for the nullable value, maybe you can do something like this:
$arrayobj = new ArrayObject([1 => 'Albuquerque', 2 => 'Farmington', 3 => 'Gallup']); //this will be your query
$default = 'Select one';
$array = (array)$arrayobj;
array_unshift($array, $default);
$arrayobj->exchangeArray($array);
return $arrayobj;
$arrayobj is just your query variable in the controller, then you can convert it to an array, prepend the non city name and then convert that array to your previous object. I hope this made sense.
As to forgetting to close the method -- color me red-faced. I should have caught that!
For the nullable value, I'm afraid you lost me on that. But I'm not going to worry about it. Maybe later. That's pretty low on the priority list, and for now I'll simply make that field not nullable :)
Cheers.
$cities = Cities::all(); //Not sure what you use to build your list
$null = 'Select one';
$turn_object_into_array = (array)$cities; //Turns your object into an array
array_unshift($turn_object_into_array, $null); //Takes the null field and puts it at the beginning of the stack
$cities->exchangeArray($turn_object_into_array); //This swaps out your cities object with the newly shuffled array
return View::make('route', compact($cities));
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community