Laravel - 使用Eloquent查询buider在select中添加自定义列

时间:2021-05-22 20:18:44

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']);

3 个解决方案

#1


16  

Simplest would be to use DB::raw

最简单的方法是使用DB :: raw

     DB::table('modules')->get(['url', DB::raw('1 as active')]);

#2


2  

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 closure or Builder. In your case it will be:

我们可以使用\ Illuminate \ Database \ Query \ Builder :: selectSub方法的第一个参数作为原始SQL或Closure或\ Illuminate \ Database \ Query \ Builder在select中添加子查询或“自定义列”。更好的解决方案是关闭或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的对象。准备好的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 for modules,我们需要获取模块的url和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:

或者你可以用raw 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 columns necessarily to use select('*'):

要使所有列必须使用select('*'):

App\Module::select('*')->selectSub($sql, 'text')->get();

Not:

App\Module::selectSub($sql, 'text')->get();

#3


-1  

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']);

#1


16  

Simplest would be to use DB::raw

最简单的方法是使用DB :: raw

     DB::table('modules')->get(['url', DB::raw('1 as active')]);

#2


2  

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 closure or Builder. In your case it will be:

我们可以使用\ Illuminate \ Database \ Query \ Builder :: selectSub方法的第一个参数作为原始SQL或Closure或\ Illuminate \ Database \ Query \ Builder在select中添加子查询或“自定义列”。更好的解决方案是关闭或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的对象。准备好的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 for modules,我们需要获取模块的url和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:

或者你可以用raw 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 columns necessarily to use select('*'):

要使所有列必须使用select('*'):

App\Module::select('*')->selectSub($sql, 'text')->get();

Not:

App\Module::selectSub($sql, 'text')->get();

#3


-1  

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']);