Select box with first option empty

妖精的绣舞 提交于 2019-12-03 06:41:16

问题


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 "Please select one option".


回答1:


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.




回答2:


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



回答3:


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>



回答4:


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



回答5:


This worked for me on Laravel 5.4.

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



回答6:


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




回答7:


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



回答8:


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



回答9:


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



回答10:


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




回答11:


Adding to Laerte anwser

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

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



回答12:


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



回答13:


{{ Form::select('parent_id', [null=>'Please Select'] + \App\Item::where('1','1')->pluck('name', 'id')->toArray()) }}



回答14:


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




回答15:


{ !! 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.



来源:https://stackoverflow.com/questions/24912295/select-box-with-first-option-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!