php Laravel - 使用 Eloquent 查询构建器在选择中添加自定义列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27143652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Add custom column in select with Eloquent query buider
提问by ZarkDev
this is a simplified use case, only to illustrate what I want to achieve:
这是一个简化的用例,只是为了说明我想要实现的目标:
Considering this query in pure SQL:
在纯 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']);
回答by turntwo
Simplest would be to use DB::raw
最简单的方法是使用 DB::raw
DB::table('modules')->get(['url', DB::raw('1 as active')]);
回答by Artem Molotov
We can add subquery or "custom column" in select with first argument of \Illuminate\Database\Query\Builder::selectSub
method as raw SQL
or Closure
, or \Illuminate\Database\Query\Builder
. Better solution is closureor Builder
.
In your case it will be:
我们可以在 select 中添加子查询或“自定义列”,\Illuminate\Database\Query\Builder::selectSub
方法的第一个参数为raw SQL
or Closure
, or \Illuminate\Database\Query\Builder
。更好的解决方案是关闭或Builder
. 在您的情况下,它将是:
$modules = DB::table('modules')->select('url')
->selectSub(function ($query) {
$query->selectRaw('1');
}, 'active')
->get();
Tested on Laravel 5.5
. In closure $query
is a object of \Illuminate\Database\Query\Builder
for subquery. Prepared SQL will be:
上测试Laravel 5.5
。在闭包中$query
是一个\Illuminate\Database\Query\Builder
for 子查询的对象。准备好的 SQL 将是:
select `url`, (select 1) as `active` from `modules`
Extended example... If we use App\Module
eloquent for modules and we need get url
of modules and count
of their submodules with id > 5
, we can write next:
扩展示例...如果我们App\Module
对模块使用eloquent 并且我们需要使用 获取url
模块count
及其子模块id > 5
,我们可以写下一个:
$modules = App\Module::select('url')
->selectSub(function ($query) {
/** @var $query \Illuminate\Database\Query\Builder */
$query->from('submodules')
->selectRaw('COUNT(*)')
->where('id', '>', 5)
->whereRaw('`modules`.`id` = `submodules`.`module_id`');
}, 'countOfSubModules')
->get();
Prepared SQL will be:
准备好的 SQL 将是:
select `url`,
(select COUNT(*) from `submodules`
where `id` > ? and `modules`.`id` = `submodules`.`module_id`)
as `countOfSubModules`
from `modules`
Or you can write your example with raw sql:
或者您可以使用原始 sql编写示例:
$sql = 'SELECT 1';
$modules = DB::table('modules')->select('url')->selectSub($sql, 'active')->get();
Then prepared SQL will be:
然后准备好的 SQL 将是:
select `id`, (SELECT 1) as `active` from `modules`
For get all columnsnecessarily to use select('*')
:
为了让所有列都必须使用select('*')
:
App\Module::select('*')->selectSub($sql, 'text')->get();
Not:
不是:
App\Module::selectSub($sql, 'text')->get();
回答by Pupil
Laravel Eloquent has very flexible query builder.
Laravel Eloquent 有非常灵活的查询构建器。
You can specify a column to return as:
您可以指定要返回的列:
$users = DB::table('modules')->select('1 as active')->get(['url']);