Laravel - Add custom column in select with Eloquent query buider
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Adding Custom Columns in Select Statements with Laravel Eloquent Query Builder
Introduction
When working with databases in Laravel applications, the query builder is a powerful tool that allows you to abstract and simplify your database interactions. You can easily manipulate data using various methods provided by the query builder, such as selecting specific columns or applying complex joins. However, when it comes to adding custom columns while retaining the simplicity of Eloquent queries, things might seem a bit tricky at first. In this blog post, we'll learn how to add custom columns seamlessly using Laravel's query builder and Eloquent models.
The Scenario
You are working on a project where you need to fetch the URL of each module from your database without worrying about its active status. The data is stored in a table named "modules" with two columns: "url" and "active". In this case, we want to append an "active" column to our query results that will contain either 0 or 1 indicating whether the module is active or not.
Solution 1: Using Raw SQL Queries
The most straightforward approach would be to use raw SQL queries. By adding a constant '1' as the value for the active column, we can easily identify which modules are active by checking its value in our application code. A sample query could look like this:
SELECT url, 1 AS active
FROM `modules`
WHERE 1
This solution provides a clean and efficient approach for the given use case but relies on raw SQL queries instead of Eloquent models. It might not be practical in scenarios that require multiple database interactions or where you need better data manipulation capabilities.
Solution 2: Adding Custom Columns with Query Builder
To achieve the same result while maintaining the convenience provided by Eloquent query builder, we can use the 'addSelect()' method, like this:
DB::table('modules')
->select(['url', DB::raw('CASE WHEN active = 1 THEN 1 ELSE 0 END AS active')])
->get();
This query will select both the "url" column and a custom "active" column based on the value of the "active" field in the database table. The 'CASE' statement checks if the 'active' field is equal to 1, and sets the active column accordingly. The result should look similar to this:
[{
"url": "https://www.example.com/",
"active": 1
}, {
"url": "https://www.example2.com/",
"active": 0
}]
Conclusion
In this blog post, we discussed two ways to add custom columns in select statements using Laravel's query builder and Eloquent models. While the first solution is straightforward but limited to specific cases, the second one offers more flexibility for your database interactions. By leveraging advanced SQL queries with the 'CASE' statement, you can easily manipulate data to suit your application needs. Regardless of which option you choose, always keep in mind that the best approach depends on your project requirements and existing codebase.