Select box with first option empty

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

    Adding to Laerte anwser

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

    {!! Form::select('entity', $entityArray) !!}
    
    0 讨论(0)
  • 2021-02-01 16:53

    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

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

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