Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Aronaman's avatar

DB query grouping sum each

i want to achieve

*OrgName #1
        - PayPal
            - 2020-01-01 
                    [ 
                    array_booking
                    ]
                - Count Total (LBP)
                - Total Sum(LBC) commison
            - 2020-01-02
                    [ 
                    array_booking
                    ]
                - Count Total (LBP)
                - Total Sum(LBC) commison
            - 2020-01-04
                .
                .

        - credit Card
            - 2020-01-02
                    [ 
                    array_booking
                    ]
                - Count Total (SBP)
                - Total Sum(SBC) commison


    *OrgName #2

bookings table

id |org_id | local_id| check_in| Checkout| commision| Pay| Payment_gateway| create_at| update_at|

my query i just try

$bookedStatus_id=BookStatus::where('name','Booked')->first()->id;
 $booking=Booking::with('bookable')->where('book_status_id',$bookedStatus_id)
    				->where('pay','>',0)
    				->selectRaw("DATE_FORMAT(Update_date, '%Y %m %e') date")
    				->get()
    				->groupBy(['organization_id','payment_gateway','date'])
    				->toArray();

// db query also try 
$booking=DB::table('bookings')->where('book_status_id',$bookedStatus_id)
    			->where('pay','>',0)
    			 ->selectRaw("DATE_FORMAT(check_in, '%Y %m %e') date")
    			->selectRaw("*,SUM(commison) as sum")					  
    			->get()
    			 ->groupBy(['organization_id','payment_gateway','date'])
    									  
    			 ->toArray();

not working for me any help, please

0 likes
6 replies
bobbybouwmann's avatar

Do you have a RAW query that is working for you? It's easier to start with that and then migrate to Eloquent

bobbybouwmann's avatar

@aronaman I suggest you create a raw query first and post it here. I don't 100% understand what you're trying to achieve.

Aronaman's avatar

@bobbybouwmann

I am new to db builder ,,,what i am wanting is grouping bookings table by the" org_id", "payment_gateway" and "update_at". and on each date count all array and sum commissions column value for every date.

i hope now is clear. can you give me any code example?

bobbybouwmann's avatar

You want a lot from one query. I'm not even sure if this is possible at all, let alone in Eloquent. That's why I ask for a raw query. This way you can figure out what exactly you need and if it's possible.

Please or to participate in this conversation.