I'm trying to make a query in Laravel, but it's not working. I USE 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;
I also tried the following.
$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();
 
     
    