Laravel dynamic dropdown country and state

Laravel dynamic dropdown country and state

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel Dynamic Dropdown Country and State

Implementing dependent dropdowns for countries and states is a common requirement in user registration forms, but the initial approach often leads to inefficient database queries and fragile frontend logic. The core issue lies in how data is loaded and passed to the view. In the provided example, the controller fetches all records from both the countries and states tables independently using eager loading or simple retrieval. While this works for static data, it creates a significant performance bottleneck at scale. Loading every state into memory just to filter them based on user selection in the browser is an architectural anti-pattern.

The solution requires shifting the responsibility of filtering from the client-side JavaScript (or complex Blade logic) to the server-side controller. By querying only the necessary data, you ensure that the response payload contains exactly what the view needs without unnecessary overhead.

Restructuring the Controller Logic

Instead of fetching all states and all countries separately, you should load the countries first, then eager-load their associated states. This leverages Laravel's query builder relationships to optimize the database hit.

Here is how the controller method should be structured:

public function user_register()
{
    // Fetch countries with their related states eager-loaded
    $countries = Country::with('states')->get();

    $title = ucwords(Lang::get('constants.user') . ' ' . Lang::get('constants.register'));
    
    return View::make('register.user_register')
        ->with('title', $title)
        ->with('page', 'user_register')
        ->with('countries', $countries);
}

In this approach, we assume a Country model with a relationship defined to the State table via country_id. By passing $countries to the view, each country object already contains a collection of its valid states. This eliminates the need for nested loops that iterate through an entire list of states just to find the ones belonging to a specific country.

Optimizing the Blade View

With the data structure adjusted, the Blade template needs to reflect this hierarchy. The countries dropdown remains largely similar, but the logic for populating the states dropdown must change. Instead of looping through a flat list of all states, you must iterate only through the states associated with the currently selected country.

To handle this effectively in Blade, it is cleaner to bind the data directly rather than constructing options manually inside loops if possible, but given the legacy structure in the source material, here is how to adapt the manual iteration:

<select class="form-control" id="countries" name="countries">
    <option value="">Please select</option>
    @foreach($countries as $country)
        <option value="{{ $country->sortname }}" {{ old('countries') == $country->sortname ? 'selected' : '' }}>
            {{ $country->sortname }} - {{ $country->name }}
        </option>
    @endforeach
</select>

<select class="form-control" id="states" name="states">
    <option value="">Please select</option>
    
    {{-- Determine the currently selected country to filter states --}}
    @if(isset($selectedCountry))
        @foreach($selectedCountry->states as $state)
            <option value="{{ $state->name }}" {{ old('states') == $state->name ? 'selected' : '' }}>
                {{ $state->name }}
            </option>
        @endforeach
    @endif
</select>

A common pitfall in the original code is attempting to access $countryname->name inside the state loop without defining which country is currently selected. In a dynamic scenario, you cannot simply iterate over all states because that ignores the relationship constraint. The view needs to know the context of the selection before rendering the second dropdown.

If you prefer to keep the logic entirely on the frontend using JavaScript for better performance with massive datasets (thousands of countries), you would need to send the country data as JSON and handle the filtering client-side. However, for typical user counts, server-side rendering via relationships is more robust and secure.

Handling Form Submission

When the form is submitted, ensure your controller validates that a state was selected only if a country was chosen. You might see validation rules like:

$rules = [
    'countries' => 'required',
    'states' => 'required_if:countries,*' // State required if country is not empty
];

This ensures data integrity at the application level, preventing users from submitting a valid state code that belongs to a different country or no country at all.

Our network