如何在laravel中进行此查询?

时间:2019-06-16 19:05:37

标签: laravel postgresql

我正在尝试在Laravel中进行查询,但无法正常工作。我使用POSTGRESQL

select extract(year from fecha_creacion) as anio, extract(month from fecha_creacion) as mes,
     sum(case when tipo = 'entrada' then 1 else 0 end ) 
  from documento 
  group by extract(year from fecha_creacion), extract(month from fecha_creacion)
  order by anio, mes;

我也尝试了以下方法。

$data = DB::table('documento')
    ->selectRaw('year from fecha_creacion AS anio, month from fecha_creacion AS mes')
    ->sum(case when tipo = 'entrada' then 1 else 0 end )
    ->orderBy(anio, mes, DESC)
    ->get();

1 个答案:

答案 0 :(得分:0)

您可以使用laravel DB::raw()表达查询。

您必须在原始函数中传递查询。

参考:https://laravel.com/docs/5.8/queries#raw-expressions