Laravel Select Dropdown from Database Dyanamically example; In this tutorial you will learn how to display the select options with value in laravel application;
This tutorial explained you to create dropdown from database value seleted dynamically in laravel. You can use show dropdown select box with options and selected selectbox in laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 apps.
In Controller
public function countryDropdown($id)
{
$countries = Country::all();
$selectedID = 2;
return view('users.edit', compact('selectedID', 'countries'));
}
In Blade File
Using Form:
<div class="form-group">
{!! Form::Label('Country', 'Country:') !!}
{!! Form::select('country_id', $countries, $selectedID, ['class' => 'form-control']) !!}
</div>
Without Using Form:
<select class="form-control" name="country_id">
<option>Select Country</option>
@foreach ($countries as $key => $country)
<option value="{{ $country->id }}" {{ ( $selectedID == $country->id) ? 'selected' : '' }}>
{{ $country->name }}
</option>
@endforeach
</select>
Selected Value with Old Value Retain
<div class="form-group">
<strong>Country:</strong>
<select name="country" class="form-control custom-select">
<option value="">Select Country</option>
@foreach($countries as $country)
<option value="{{ $country->id }}" @if(old('country') == $country->id || $country->id == $user->country) selected @endif>{{ $country->name }}</option>
@endforeach
</select>
</div>
See Also: How to Set Select Box Selected Option In Laravel
See Also: Retain Old value in multiple select option in laravel
See Also: How to Use Select 2 Dropdown in Laravel for Multiple Select