Laravel Select Dropdown From Database Example

Laravel 6 , Laravel , Laravel 7

Nicesnippets

165903

25-05-2020


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


Recommended Posts