Skip to content
Advertisement

Laravel – Add custom column in select with Eloquent query buider

this is a simplified use case, only to illustrate what I want to achieve:

Considering this query in pure SQL:

SELECT url, 1 AS active
FROM  `modules` 
WHERE 1 

How can I add the constant active column using query builder ?

Here is my Query Builder without the extra column:

DB::table('modules')
->get(['url']);

Advertisement

Answer

Simplest would be to use DB::raw

     DB::table('modules')->get(['url', DB::raw('1 as active')]);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement