Laravel - Set Selected Option in Dropdown Menu Example

By Hardik Savani November 5, 2023 Category : PHP Laravel

I will explain how to populate select box with selected option dynamically in laravel. You can do it dropdown from database with specific value selected in your html blade file, even if you didn't use Form Class.

we almost use Form Composer package for generate html form. Form facade will help to create text box, radio button, select box etc. you can easily create dynamic select box with Form class. you can make it simply selected value from argument without if condition.

I will give two way to make selected option on drop down menu in laravel. So let's see both example and you can use in your laravel 5, laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 project.

Controller Code:

/**

* Show PDF

*

* @return \Illuminate\Http\Response

*/

public function consentFormListShowPDF(Request $request)

{

$products = Product::pluck('name', 'id');

$selectedID = 2;

return view('stock.edit', compact('id', 'products'));

}

Example 1 Using Form:

<div class="form-group">

{!! Form::Label('product', 'Product:') !!}

{!! Form::select('product_id', $products, $selectedID, ['class' => 'form-control']) !!}

</div>

Example 2 Without Using Form:

<select class="form-control" name="product_id">

<option>Select Product</option>

@foreach ($products as $key => $value)

<option value="{{ $key }}" {{ ( $key == $selectedID) ? 'selected' : '' }}>

{{ $value }}

</option>

@endforeach

</select>

I hope it can help you....

Shares