Reputation: 497
How can I set the first option in my select box to an empty value?
I'm getting the data from my DB, and I would like to set the option by default as "Please select one option".
Upvotes: 33
Views: 69368
Reputation: 646
In the view
{!! Form::select('qualification_level', $qualification , old('Qualification_Level'), ['class' => 'form-control select2', 'placeholder' => '']) !!}
In the Controller $qualification = \App\Qualification::pluck('name','id')->prepend('Please Select');
Upvotes: 2
Reputation: 7083
For anyone that needs this behavior, this way is working fine:
Controller:
$entityArray = Entity::lists('name', 'id');
$entityArray->prepend('Select', 'Select');
View:
{!! Form::select('entity', $entityArray) !!}
Upvotes: 16
Reputation: 827
I found that 'default'=>'Please select'
doesn't work with the HTML5 required attribute.
This does work:
$listOfValues = [1 => 'Choice 1'];
Form::select('fieldname',[null=>'Please Select'] + $listOfValues);
If you don't like modern PHP syntax,
$listOfValues = array(1 => 'Choice 1');
$listOfValues[null] = 'Please Select';
Form::select('fieldname', $listOfValues);
But the point is to have a label for the null value.
Upvotes: 51
Reputation: 7391
{{ Form::select('parent_id', [null=>'Please Select'] + \App\Item::where('1','1')->pluck('name', 'id')->toArray()) }}
Upvotes: 0
Reputation: 71
This worked for me on Laravel 5.4.
{{ Form::select('agency', $agency, null, [
'placeholder' => 'Please select ...',
'class' => 'form-control'
]) }}
Upvotes: 7
Reputation: 2763
In laravel 5.2
This worked for me
{!! Form::select('user', $users, null, array('class'=>'form-control', 'placeholder' => 'Please select')) !!}
as I just add placeholder
and it made the trick
Upvotes: 3
Reputation: 21
You need to manipulate the array before the view
or to be messy you could do it in blade @php tags
$users= [null => 'Empty'];
$dbusers= User::pluck('id', 'name');
$users= array_merge($users, $dbusers->toArray());
return view('myview', compact('users'))
and then you can do the following in the view
{{ Form::select('user',$users, ['class' => 'form-control']) }}
Upvotes: 0
Reputation: 41
100% result:
In controller:
$users = App\User::get()->lists('full_name', 'id')->prepend('Select user','');
return view('name of view')->with('users', $users);
In view:
{!! Form::select('who', $users, null, ['class' => 'form-control inline']) !!}
I´m using "laravelcollective/html":"^5.3.0" package
Upvotes: 4
Reputation: 389
in controller
$data['options']=Entity::pluck('name','id')->prepend('Please Select','');
return view('your_view_blade',$data);
in view blade
{!! Form::select('control_name',$options,null,['class'=>'your_class']) !!}
Upvotes: 5
Reputation: 475
If you are using the HTML package by LaravelCollective, you do the following..
Form::select('size', array('L' => 'Large', 'S' => 'Small'), null, ['placeholder' => 'Pick a size...']);
Upvotes: 32
Reputation: 51
Adding to Laerte anwser
You can do it at the Blade level
just by issuing a command:
{!! Form::select('entity', $entityArray) !!}
Upvotes: 0
Reputation: 1068
In Laravel 5.1 i solved it by doing
$categories = [''=>''] + Category::lists('name', 'id')->toArray();
return view('products.create', compact('categories'));
Or
$categories = [''=>''] + Category::lists('name', 'id')->all();
return view('products.create', compact('categories'));
Upvotes: 2
Reputation: 82
{ !! Form::select('country', $country, 'GB', ['id' = > 'country', 'class' = > 'form-control select2me']) !!}
Here $country
is array that contains many countries , in this array is "great britain" by the id "GB" which will be selected by default.
Upvotes: -1
Reputation: 21
For a Laravel 5 collection, you may need to convert the collection to array first.
<?php
$defaultSelection = [''=>'Please Select'];
$users = $defaultSelection + $users->toArray();?>
and apply $users as
{!! Form::select('user', $users); !!}
Upvotes: 2
Reputation: 1362
There are 2 methods to do this:
{{ Form::select('user', array('default' => 'Please select one option') + $users, 'default') }}
Or
<select>
<option selected disabled>Please select one option</option>
@foreach($users as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</select>
Upvotes: 22