1

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?

Limon Monte
  • 52,539
  • 45
  • 182
  • 213
Ali Erfani
  • 682
  • 1
  • 11
  • 27

2 Answers2

3

The listsmethod 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.

Ali Erfani
  • 682
  • 1
  • 11
  • 27
2

I don't know about having it submit as null. If you put a blank one in there, it will just come back as an empty string so you would have to check that it's empty and if it is, set it to null.

To add it to the array, simply add it to the beginning and it should work.

$cats = ['', 'SDL Trados', 'Deja Vu', 'WordFast', 'OmegaT', 'Fluency'];

Or if you already have the $cats variable and you need to modify it...

array_unshift($cats, '');

You may have to key these arrays though because I believe the select is going to set the keys as the text and the values as the value in the option elements.

In that case, you will want to still use array_unshift and pass in an array instead of a string.

array_unshift($languages, ['Optional' => '']);`

That will add an additional option to the select with the test Optional and no value.

user1669496
  • 32,176
  • 9
  • 73
  • 65
  • Thanks. What if this array is returned from a database table? – Ali Erfani Sep 01 '15 at 12:57
  • 1
    Rather than `get()`, you want to use `lists(value, key)` which will return the array in the appropriate format to use for your select boxes where value is the name of the database column which stores the values (probably the id) and key would be the column you'd grab for the text of the option like name or description. For example `lists('id', 'name')` – user1669496 Sep 01 '15 at 13:05
  • As you can see in the edited version of my question, I'm doing the same thing – Ali Erfani Sep 01 '15 at 13:15
  • I've modified my answer – user1669496 Sep 01 '15 at 13:32
  • Sorry, but the $languages is in fact a collection not an array and the array_unshift need an array! – Ali Erfani Sep 01 '15 at 13:46