简体   繁体   中英

Select box with first option empty

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".

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.

如果您使用LaravelCollective 的 HTML 包,请执行以下操作。

Form::select('size', array('L' => 'Large', 'S' => 'Small'), null, ['placeholder' => 'Pick a size...']);

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>

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) !!}

This worked for me on Laravel 5.4.

{{ Form::select('agency', $agency, null, [
    'placeholder' => 'Please select ...',
    'class' => 'form-control'
]) }}

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']) !!}

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

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

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.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'));

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');

Adding to Laerte anwser

You can do it at the Blade level just by issuing a command:

{!! Form::select('entity', $entityArray) !!}

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']) }}
{{ Form::select('parent_id', [null=>'Please Select'] + \App\Item::where('1','1')->pluck('name', 'id')->toArray()) }}
{ !! Form::select('country', $country, 'GB', ['id' = > 'country', 'class' = > 'form-control select2me']) !!}

这里$country是包含许多国家的数组,在这个数组中是由 id "GB"默认选择的"great britain"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM