Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 233
user3189734 Avatar asked Sep 10 '25 16:09

user3189734


1 Answers

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');
like image 175
Saranga A Avatar answered Sep 12 '25 06:09

Saranga A