Does anybody know how can I set in my select box the first option to empty value?
I\'m getting the data from my DB, and I would like to set the option by default as \"P
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); !!}
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
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'));
{ !! 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.
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) !!}
{{ Form::select('parent_id', [null=>'Please Select'] + \App\Item::where('1','1')->pluck('name', 'id')->toArray()) }}