如何使用laravel profulent编写带“with”查询语句

ki1q1bka  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(77)

如何使用laravel进行查询,类似下面的示例谢谢

with s as (select s.id from s)
select a.*
from a
left join s on s.id = a.s_id
...
ddrv8njm

ddrv8njm1#

Eloquent不支持WITH子句。您可以传入原始查询

$sql = <<<SQL
with s as (select s.id from s)
select a.*
from a
left join s on s.id = a.s_id
...
SQL;

$results = DB::select($sql);

或者使用添加了此功能的软件包,如staudenmeir/laravel-cte

DB::query()
    ->withExpression('s', fn ($query) => $query->select('s.id')->from('s'))
    ->select('a.*')
    ->from('a')
    ->leftJoin('s', 's.id', 'a.s_id')
    ...

相关问题