Search code examples
phplaravellaravel-4

Add default value to select list in Laravel form::select


Simple question, I hope.

I need to add a default value to my select list 'Please Select', and set it to disabled.

<select name="myselect" id="myselect">
  <option value="" disabled>Please Select</option>
  <option value="1">Item 1</option>
  <option value="2">Item 2</option>
</select>

My current laravel form::select is

{{
Form::select(
    'myselect',
    $categories,
    $myselectedcategories,
    array(
        'class' => 'form-control',
        'id' => 'myselect'
    )
}}

How can I amend this to include the default option value?


Solution

  • You can use array_merge like this:

    {{
    Form::select(
        'myselect',
        array_merge(['' => 'Please Select'], $categories),
        $myselectedcategories,
        array(
            'class' => 'form-control',
            'id' => 'myselect'
        ))
    }}
    

    Alternatively you can set the placeholder somewhere before the select:

    $categories[''] = 'Please Select';
    

    Update

    To add the disabled attribute you can try this: (untested)

    {{
    Form::select(
        'myselect',
        array_merge(['' => ['label' => 'Please Select', 'disabled' => true], $categories),
        $myselectedcategories,
        array(
            'class' => 'form-control',
            'id' => 'myselect'
        ))
    }}