How to Populate Dropdown List From Database With Laravel

In this post, we are going to create HTML select element (dropdown list) and populate it with values extracted from database.

Lets assume that we have a countries table. It contains a list of countries with ids as primary key, which you would like to display in HTML select box list.

Quick Tip: Change default primary key in Eloquent

To build a select box element we are using Illuminate Form and HTML builder package.


First you need to extract the list of countries from database and pass

public function create()
    $countries = \DB::table('countries')->lists('name', 'id');
    return view('dashboard.create')->with('countries', $countries);

As you can see, we are using lists method to create an array which contains names and country ids. And we are using with method to pass these values to the create view.


To output the values stored inside the countries array, we need pass it to select method as the second parameter.

<div class="form-group">
    {!! Form::label('country_id','Select a Country') !!}
    {!! Form::select('country_id', $countires, null, ['class' => 'form-control']) !!}

The above code will produce following HTML output,

<div class="form-group">
    <label for="country_id">Select a Country</label>
    <select name="category" class="form-control">
        <option value="1">India</option>
        <option value="2">China</option>
        <option value="3">Russia</option>
        <option value="4">Australia</option>



Leave a Comment.