atfinho
6 months ago
70
10
Laravel

Sum returns double value

Posted 6 months ago by atfinho

Hey guys , I have this query below where I want to get sum total_price value from a table called " car_maintenance" which is related to " bookings" the problem is that sum returns me double value..

this is the query

                                      $filter=DB::table('bookings')
                                        ->groupBy('cars.model')
                                        ->groupBy('cars.daily_price')
                                        ->join('cars','bookings.car_id','=','cars.id')
                                        ->leftJoin('car_maintenance','car_maintenance.car_id','=','bookings.car_id')
                                        ->select('cars.model',
                                            DB::raw('sum(DATEDIFF(bookings.to,bookings.from)) as bookingDays'),
                                            DB::raw('365-sum(DATEDIFF(bookings.to,bookings.from)) as parkingStay'),
                                            DB::raw('cars.daily_price*sum(DATEDIFF(bookings.to,bookings.from)) as gains'),
                                            DB::raw('sum(car_maintenance.total_price) as losts'))
                                            ->get();

Thank you

Please sign in or create an account to participate in this conversation.