Select box with first option empty

后端 未结 15 1062
情深已故
情深已故 2021-02-01 16:29

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

相关标签:
15条回答
  • 2021-02-01 16:43

    This worked for me on Laravel 5.4.

    {{ Form::select('agency', $agency, null, [
        'placeholder' => 'Please select ...',
        'class' => 'form-control'
    ]) }}
    
    0 讨论(0)
  • 2021-02-01 16:45

    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>
    
    0 讨论(0)
  • 2021-02-01 16:47

    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...']);
    
    0 讨论(0)
  • 2021-02-01 16:48

    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.

    0 讨论(0)
  • 2021-02-01 16:48

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

    0 讨论(0)
  • 2021-02-01 16:50

    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']) !!}
    
    0 讨论(0)
提交回复
热议问题