Hello Devs,

In this tutorial, we are going to learn how to show select box with selected option dynamically in laravel.


Controller Code

/**

* Show records

*

* @return \Illuminate\Http\Response

*/



public function dropDownShow(Request $request)

{

   $items = Item::pluck('name', 'id');

   $selectedID = 2;

   return view('items.edit', compact('id', 'items'));

}



Example 1 : Using Form

<div class="form-group">
    {!! Form::Label('item', 'Item:') !!}
    {!! Form::select('item_id', $items, $selectedID, ['class' => 'form-control']) !!}
</div>


Example 2 : Without Using Form

<select class="form-control" name="product_id">
    <option>Select Item</option>
    @foreach ($items as $key => $value)
        <option value="{{ $key }}" {{ ( $key == $selectedID) ? 'selected' : '' }}> 
            {{ $value }} 
        </option>
    @endforeach    
</select>


I hope this example helps you.