Eloquent column list by key with array as values?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: A Comprehensive Guide to Retrieving Unique Keys with Values Using Eloquent Array in Laravel
Body: In the world of web development, one of the most essential features is data management. Databases store large volumes of information and are frequently accessed by applications to retrieve relevant data. When working with databases, developers often need to manipulate this data for analysis or presentation purposes. Eloquent, Laravel's Object-Relational Mapping (ORM) library, offers a convenient way to interact with database tables through models.
One common task is retrieving all the distinct keys and their associated values in a particular column within a table. However, the given code snippet only returns a single column, which might not always be sufficient for our needs. In this blog post, we'll explore how to retrieve an array of unique keys with their respective values using Eloquent.
The first approach involves using a simple query builder to handle the task:
$roles = DB::table('roles')->lists('title', 'name');
This code snippet generates an array with two keys, "title" and "name", containing the distinct values found in those columns. However, suppose we need to include another column like "permission_levels" along with the previous two. To achieve this, we could modify the query builder using arrays for our desired output:
$roles = DB::table('roles')->lists(['title', 'name'], 'role');
This will return an array with two keys ('title' and 'name') and a single value ('role') containing the distinct values from all the columns mentioned in the lists() method. But what if we need to include another column while retrieving these unique values? We can add that as well:
$roles = DB::table('roles')->lists(['title', 'name'], 'role', ['permission_levels' => null]);
This query will now generate an array with three keys ('title', 'name', and 'permission_levels'), each containing the respective unique values. However, in this case, we have specified a default value for 'permission_levels' as 'null'. This is because Eloquent doesn't support returning columns without specifying at least one column to list. If you don't want any values for a specific field or just wish to omit it, you need to provide a default value like 'null', '0', or an empty array.
Alternatively, we can use Eloquent models and relationships to achieve the same result:
1. Create a new model called 'Role' that has all the columns needed for our output in your project (title, name, permission_levels).
2. In the 'Role' model, declare a relationship with a different table containing more data like 'Permissions', which we'll use as an example:
public function permissions() : HasMany
{
return $this->hasMany(Permission::class);
}
3. Create a new model called 'Permission' with columns specific to its purpose and any relationships it might have with other tables.
4. In your controller or view, query for all the unique combinations of roles:
$roles = Role::with(['permissions' => function($query) {
$query->select('role_id', DB:raw("COALESCE(value, default_value) as permission_level"));
}])->distinct()->get();
This query will generate an array of roles with their distinct permission levels. We used the COALESCE() function to provide a default value in case there are no values for that specific field, ensuring we don't get null values for the 'permission_levels'.
To conclude, retrieving unique keys and their associated values using Eloquent can be achieved with several methods. The most suitable approach depends on your project requirements and data structure. Always choose the one that optimizes your code and makes it more maintainable. For a deeper understanding of Laravel's Eloquent ORM, refer to https://laravelcompany.com/blog/mastering-eloquent-orm-in-laravel for comprehensive tutorials and examples.