Laravel-5 how to populate select box from database with id value and name value

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Populate Select Boxes from Database in Laravel 5 Using Illuminate\Html

Laravel is an incredibly powerful PHP framework that simplifies the process of building web applications. One key feature of Laravel is its integration with the Illuminate HTML library, which provides a convenient way to generate HTML forms and other elements. In this blog post, we will discuss how to efficiently populate select boxes from your database using this library.

The Problem

You've tried implementing a simple select box in your Laravel application using the Illuminate\Html package, but instead of displaying just the item names as options, all the entity information is being shown. The reason for this issue is that when you pass the entire collection of items to your view, Laravel automatically converts each item into an array with key-value pairs (id and name).

The Solution: Populating Select Boxes from Database in Laravel 5 Using Illuminate\Html

To solve this problem, you need to modify how you populate the select box. Instead of using the entire collection, we will use a nested array to specify a key that holds the id values and another key for the corresponding names. This way, Laravel will only display the associated names in the select box while maintaining the id values as hidden values.

Step 1: Modify Your Database Model

First, modify your database model to create a new attribute that holds the combined key for each item. You can use a custom accessor to concatenate the ID and name attributes as a single string value (e.g., 'id_name'). Here's an example:

class Item extends Model
{
    public function getIdNameAttribute()
    {
        return $this->id . '_' . $this->name;
    }
}
This attribute will generate a new array with keys 'item_id' and 'item_name', making it easy to loop through when populating the select box.

Step 2: Modify Your Controller Function

Next, modify your controller function to only retrieve the necessary information from the database. Instead of using Items::all(), use Items::select('id', 'name')->get(). This will return a collection with two columns - id and name:

public function create()
{
    $items = Items::select('id', 'name')->get();

    return view('prices.create', compact('id', 'items'));
}
Now, instead of a nested array with id and name as keys in each item, you will have a collection where each element is an array with the 'item_id' and 'item_name' keys.

Step 3: Modify Your View Template

Finally, modify your view template to use the new data structure. The Form::select function in Laravel accepts three arguments - the name of the array key that holds the options, the array containing the options, and an optional third argument for any additional attributes that should be added to the select element:

<div class="form-group">
    {!! Form::Label('item', 'Item:') !!}
    {!! Form::select('items', $items, null, ['class' => 'form-control']) !!}
</div>
With these modifications, the select box will now populate correctly with only the item names as options. The hidden id value will be available for future use or submission.

Conclusion

In this post, we have discussed how to efficiently populate a select box from your database in Laravel 5 using Illuminate\Html. By modifying the database model, controller function, and view template, you can achieve seamless integration between the frontend form and backend data storage.