skip to Main Content

I am learning to use the laravel eloquent and I have this query

SELECT SUM(precio_total) FROM `venta` GROUP BY (MONTH(fecha));

So far i wear this

$cantidad_venta = Venta::select('SUM(PRECIO_TOTAL) as suma_precio')
                                ->groupBy('MONTH(fecha)')
                                ->get();

I try to go through axios, but it throws me an error

return response()->json($cantidad_venta); 

I would really appreciate an answer, thank you very much

2

Answers


  1. $cantidad_venta = Venta::select(DB::raw('SUM(PRECIO_TOTAL) as suma_precio'), DB::raw('MONTH(fecha) as month'))
                                ->groupBy('month')
                                ->get();
    
    Login or Signup to reply.
  2. You can Do this in multiple ways. check the following code below :

      $ventas = Venta::select(DB::raw('SUM(PRECIO_TOTAL) as suma_precio'))
                     ->groupBy(DB::raw('MONTH(fecha)')
                      ->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search