How to show selected value from database in dropdown using Laravel?

Today, We want to share with you laravel dropdown from database.In this post we will show you how to show select box with selected option dynamically in laravel, hear for create dropdown from database value selected in laravel. we will give you demo and example for implement.In this post, we will learn about Select DropDowns Option Selected In Laravel with an example.

Laravel Select Dropdown From Database Example

Question: how to retrieve data from database whenever select in drop down list in laravel?

Laravel Controller Code

/**
* Show records
*
* @return \Illuminate\Http\Response
*/

public function dropDownShow(Request $request)
{

   $products = Product::pluck('name', 'id');
   $selectedID = 2;
   return view('products.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 you get an idea about create dynamic select box with Form class.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.