Support the ongoing development of Laravel.io →
posted 9 years ago
Blade Forms
Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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:

  1. How to include a default value?
  2. Aside from entering a blank value into my cities table, how can I include a blank option?

Gracias! (p.s. where in NM? :)

Last updated 1 year ago.
0

http://laravel.com/api/source-class-Illuminate.Html.FormBuilde...

  1. So from your code, replace Input::old('city_bldg_id') with the default value. If you redirect back with errors, Laravel is smart and will override that with the value they previously selected.
  2. Not sure exactly what you're trying to accomplish here. If you can explain it a little more I can probably help you figure it out.

Also, I am from Gallup.

Last updated 1 year ago.
0

Thanks again -- Answers:

  1. 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.

  1. The city field is nullable. So, I want a blank option in the select box that would allow null to be entered. I suppose that I could add fields to the cities table where the value is NULL, but then I'd have to do that for every state, and that doesn't seem like a very elegant option.

I love Gallup :) I'm in ABQ.

Last updated 1 year ago.
0
{{ 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.

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0
$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));
Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

zocios zocios Joined 28 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.