Laravel – Set Selected option in dropdown menu

Laravel – Set Selected option in dropdown menu

In this post we will give you information about Laravel – Set Selected option in dropdown menu. Hear we will give you detail about Laravel – Set Selected option in dropdown menuAnd how to use it also give you demo for it if it is necessary.

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 project.

Controller Code:

/**

* Show PDF

*

* @return IlluminateHttpResponse

*/

public function consentFormListShowPDF(Request $request)

{

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

$selectedID = 2;

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

}

Example 1 Using Form:

<div >

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

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

</div>

Example 2 Without Using Form:

Also see:PHP Laravel 5.7 – Create Admin Panel Example

<select 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….

Hope this code and post will helped you for implement Laravel – Set Selected option in dropdown menu. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  21  =  23

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US