-1

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();
Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
Gousikan
  • 9
  • 2

1 Answers1

0

You can use laravel DB::raw() expresssion query.

You have to pass the query in raw function.

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

hasan05
  • 904
  • 5
  • 22