Select box with first option empty

后端 未结 15 1042
情深已故
情深已故 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:29

    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); !!}
    
    0 讨论(0)
  • 2021-02-01 16:31

    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

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

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

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

    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) !!}
    
    0 讨论(0)
  • 2021-02-01 16:42
    {{ Form::select('parent_id', [null=>'Please Select'] + \App\Item::where('1','1')->pluck('name', 'id')->toArray()) }}
    
    0 讨论(0)
提交回复
热议问题