Laravel-使用雄辩的查询构建器在select中添加自定义列 [英] Laravel - Add custom column in select with Eloquent query buider
本文介绍了Laravel-使用雄辩的查询构建器在select中添加自定义列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个简化的用例,仅用于说明我想要实现的目标:
this is a simplified use case, only to illustrate what I want to achieve:
在纯SQL中考虑此查询:
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']);
推荐答案
最简单的方法是使用DB :: raw
Simplest would be to use DB::raw
DB::table('modules')->get(['url', DB::raw('1 as active')]);
这篇关于Laravel-使用雄辩的查询构建器在select中添加自定义列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文