Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Add custom column in select with Eloquent query buider

Tags:

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']);
like image 731
YannPl Avatar asked Nov 26 '14 07:11

YannPl


1 Answers

Simplest would be to use DB::raw

     DB::table('modules')->get(['url', DB::raw('1 as active')]);
like image 141
turntwo Avatar answered Nov 24 '22 05:11

turntwo