laravellaravel-5laravelcollective

Drop-Down Lists from laravelcollective?


I use dropdownlist from laravelcollective. I am wondering how I can use (Selected Default) with this if I fetch data from a database. Here follows my source code:

{!! Form::select('port',$ports,null,['class'=>'form-control']) !!}

The selected default that I want is for example (Select Port).


Solution

  • First of all create a list in your controller and use as a first element the "Please select a port" text with empty value:

    $ports =['' => 'Please Select a port'] + Port::lists('shortName','id')->toArray();
    

    After you pass it to the view use it like this:

    {!! Form::select('port_id',$ports,null,['class' => 'form-control']) !!}