-org_id,
-date,
- nobooking by localbook,
- total commision by localbooking,
- noboking by selfBook,
- total commison by selfbook and
*totalBook(sum both total).
Ok, we can do it both in the DB query or in the collection.
I find doing in the DB easier:
$bookings = DB::table('bookings')
->select([
'org_id',
DB::raw('CAST(created_at AS DATE) as day'),
DB::raw("SUM(CASE WHEN payment_gateway = 'LocalBook' THEN 1 ELSE 0 END) AS bookings_localbook_per_day"),
DB::raw("SUM(CASE WHEN payment_gateway = 'LocalBook' THEN commison ELSE 0 END) AS commison_localbook_per_day"),
DB::raw("SUM(CASE WHEN payment_gateway = 'selfBook' THEN 1 ELSE 0 END) AS bookings_selfbook_per_day"),
DB::raw("SUM(CASE WHEN payment_gateway = 'selfBook' THEN commison ELSE 0 END) AS commison_selfbook_per_day"),
])
->where('pay', '>', 0)
->groupBy(['org_id', DB::raw('CAST(created_at AS DATE)')])
->get()
->toArray();
They will be available for every date on the bookings table, for example:
In Day 1 there were 1 localbook order and no selfbook orders: there will be a record for day 1
In Day 2 there were 1 localbook order and 1 selfbook order: there will be a record for day 2
In Day 3 there were no localbook orders and 1 selfbook order: there will be a record for day 3
In Day 4 there were no localbook orders and no selfbook order: there won't be a record for day 4
If you need to have all days regardless of days with no orders from both sellers you can do it in the DB query (kind of hard, but doable), but my suggestion is handling this on the PHP side.
Regarding any reading material on SQL, unfortunately I don't have any material to recommend. Laracasts has a series on SQL, very good one, but it doesn't cover this kind of subject (aggregating over values or handling missing records).