3.1 Select Options - joserick/laravel-bootstrap-4-forms GitHub Wiki

Select Options

Param Type Default Description
$options iterable [] Options list
$valueKey string null key for value
$idKey string null key for id

Examples

With array

{!!Form::select('city', 'Choose your city')->options([1 => 'Gotham City', 2 => 'Springfield'])!!}

// If you leave the first key as a string empty or "0" it will be considered as the "placeholder".
{!!Form::select('city', 'Choose your city')->options([0 => 'Select city', 1 => 'Gotham City', 2 => 'Springfield'])!!}

With collection

$cities = collect([1 => 'Gotham City', 2 => 'Springfield'])
{!!Form::select('city', 'Choose your city')->options($cities)!!}

// With model collection
$cities = \App\City::all();
{!!Form::select('city', 'Choose your city')->options($cities)!!}

// Your model should have id and name attributes. If these keys are different,
// you can pass second and/or third parameters (you can use the second parameter to access some model acessor, also)
$cities = \App\City::all();
{!!Form::select('city', 'Choose your city')->options($cities, 'city_name', 'id_object_field')!!}

// When you are using collections, you can use prepend method
// (https://laravel.com/docs/5.8/collections#method-prepend) to add an first empty value, like "Choose your city"
$cities = \App\City::all();
{!!Form::select('city', 'Choose your city')->options($cities->prepend('Choose your city', ''))!!}

With nested arrays (Select Option Group)

{!!Form::select('cars', 'Choose your Car')->options([
    'Swedish Cars' => [1 => 'Volvo', 2 => 'Saab']
    'German Cars' => [1 => 'Mercedes', 2 => 'Audi']
])!!}