SleepWalker
SleepWalker

Reputation: 637

Add empty selection in laravel select with loop options

I want to add empty option inside the select field with loop data. Here's the code I'm using

<div class="form-group {{$errors->has('duration_id')}}">
    {!! Form::label('duration_id', 'Duration Id', ['class' => 'control-label']) !!}
    {!! Form::select('duration_id', $durations, null, ('required' == 'required') ? ['class' => 'form-control', 'required' => 'required'] : ['class' => 'form-control']) !!}
    {{$errors->first('duration_id')}}
</div>

How can I add something like "--select duration--" as the first selection?

enter image description here

Upvotes: 2

Views: 617

Answers (1)

Salim Djerbouh
Salim Djerbouh

Reputation: 11044

Add a placeholder to your form select element

Form::select('duration_id', $durations, null, ['placeholder' => '--select duration--'], ('required' == 'required') ? ['class' => 'form-control', 'required' => 'required'] : ['class' => 'form-control'])

From the docs

 Generating a Drop-Down List With an Empty Placeholder

This will create an <option> element with no value as the very first option of your drop-down.

echo Form::select('size', ['L' => 'Large', 'S' => 'Small'], null, ['placeholder' => 'Pick a size...']);

Upvotes: 2

Related Questions