26

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?

user3189734
  • 665
  • 1
  • 9
  • 27

12 Answers12

28

In Laravel 5.1 you can prepend the default item if the list is a collection (result of a Eloquent::lists())

$categories = Category::lists('name', 'id');
$categories->prepend('None');
Saranga A
  • 1,091
  • 16
  • 22
27

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'
    ))
}}
CrazyCrow
  • 4,125
  • 1
  • 28
  • 39
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • That works great. Thank you. Do you know how to add disabled to this option? – user3189734 Jan 11 '15 at 21:56
  • 1
    `array_merge` will resort the keys, if this matters to you. eg. `array_merge(['' => 'Please select'], [1 => 'key is 1', 0 => 'key is 0'])` returns `['' => 'Please select', 0 => 'key is 1', 1 => 'key is 0']`. – Lemmings19 May 02 '17 at 18:35
13

Add 'placeholder' => 'Please Select' into the Form::select.

{!!
  Form::select(
    'myselect', 
    $categories, 
    null, 
    ['class' => 'form-control', 'placeholder' => 'Please Select'])
!!}
mortalis
  • 2,060
  • 24
  • 34
CCJ
  • 131
  • 1
  • 2
3

Or just put placeholder, for example:

[
    'class' => 'form-control',
    'id' => 'myselect',
    'placeholder' => 'None'
]

That will do the trick.

ajlaracast
  • 53
  • 7
1
$categories = Category::lists('name', 'id');
$categories->prepend('None', 0);
Pranay Aryal
  • 5,208
  • 4
  • 30
  • 41
1

For, prepending Please Select with empty value

$categories = Category::lists('name', 'id');
$categories->prepend('Please Select', '');

This code will populate something like this ,

$categories[''] = 'Please Select';
$categories[0] = 'item 1',
$categories[1] = 'item 2';

Now you can use something like this:

{!! Form::select('myselect', $categories, '',['id'=>'myselect']) !!}

This is helpful for form validation also, like required.

Md Rashedul Hoque Bhuiyan
  • 10,151
  • 5
  • 30
  • 42
1

Now, Default India was select

{{ Form::select('terms_agreement_type', [null => 'Select Control'] + ['India' => 'India', 'Nepal' => 'Nepal'], 'India') }}

Kaushik shrimali
  • 1,178
  • 8
  • 15
1

You can use pluck

Controller

$role = Model::all();

{{ Form::select('nameselect', $role->pluck('name', 'id')->prepend('default value...', ""))}}
Das_Geek
  • 2,775
  • 7
  • 20
  • 26
rickslayer
  • 29
  • 1
0

i put my solution for this post. I hope can help to somebody

i use a php function to add an option to the array of the model

array_unshift($model, ['value' => '', 'name' => 'Select value']);

0

i have used placeholder and it worked for me

{!! Form::select('supplier', $suppliers, null, ['class' => 'form-control', 'placeholder' => 'Please Select']) !!}

Shuhad zaman
  • 3,156
  • 32
  • 32
0

Although this doesn't answer this question's description, searching the title brought me here. The third parameter of Form::select() is what dictates the default selected value.

{!! Form::select('event_id', $events, $defaultSelectedEventId) !!}
Mitch
  • 51
  • 2
-1

Laravel 5.3

{{ Form::select('role', ['' => 'Select Role'] +$roles, null, ['class' => 'form-control']) }}
JJJ
  • 32,902
  • 20
  • 89
  • 102
Gaetano
  • 39
  • 2