Laravel Select Dropdown From Database Example
In this tutorial, I will learn you how to show select box with selected option dynamically in laravel. create dropdown from database value selected in laravel.
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 dropdown menu in laravel. So Let's see both example and you can use in laravel project.
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>
It will help you...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial