Laravel dynamic dropdown country and state
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Dynamically Display Countries and States with Laravel Dropdowns
Introduction: In your web application, you have created two dropdown menus to select countries and states from your database. You are currently facing an issue where when you choose a country, its associated states don't appear in the second dropdown menu. This blog post will provide an in-depth solution to this problem.
Step 1: Retrieve Country and State Data from the Database
Firstly, you need to retrieve all the country names and state names from your database. In your AdminController.php file, add the following code snippet to get the data:
public function user_register()
{
$countryname = DB::table('countries')
->get();
$statename = DB::table('states')
->get();
...
}
Step 2: Use Ajax to Dynamically Update the State Dropdown
To make the state selection dependent on the chosen country, we can use AJAX. You can replace the HTML code for both dropdowns with the following snippet in your user_register.blade.php file:
<select class="form-control" id="countries" name="countries">
<option value="">Please select</option>
<?php foreach ($countryname as $key=>$countryname): ?>
<option value="<?php echo $countryname->sortname; ?>"<?php
if (isset($countries) && Input::old('countries') == $countryname->sortname)
{
echo 'selected="selected"';
}
?>>
<?php echo $countryname->sortname ." - ". $countryname->name ; ?>
</option>
<?php endforeach; ?>
</select>
<select class="form-control" id="states" name="states">
<option value="">Please select</option>
<?php foreach ($statename as $key=>$statename): ?>
<option value="<?php echo $countryname->name; ?>" <?php
if (isset($states) && Input::old('states') == $statename->name)
{
echo 'selected="selected"';
}
?>>
<?php echo $statename->name; ?></option>
<?php endforeach; ?>
</select>
In the above code, we have used the HTML