Laravel - Set Selected option in dropdown menu
By Hardik Savani | January 21, 2019 | | 2716 Viewer | Category : PHP LaravelI 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 \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....
Featured Post
- How to find nearest location using latitude and longitude in laravel
- Laravel redirect back with input and error message example
- Laravel 5 - Simple CRUD Application Using ReactJS - Part 1
- How to use DB transactions in laravel 5?
- Example of unionAll in Query Builder Laravel 5
- How to store all record in laravel 5 Cache
- How to add text on image in laravel 5?
- How to get last executed mysql query in laravel ?
- How to set URL without http of Other site in Laravel?