I want to list the revenue of all businesses. All businesses are meant to be listed whether they have revenue or not. The revenue can also be sorted by date (within a date range). Meaning that I can check how much each of the business has made between certain dates.
Here is my implementation below:
return Business::
leftJoin('invoice as i', 'i.business_id', '=', 'businesses.id')
->leftJoin('departments as d', 'businesses.department_id', '=', 'd.id')
->where('i.created_at', '>', $from)
->where('i.created_at', '<', $to)
->select('businesses.id','businesses.name', 'd.name as department', 'd.id as deparment_id', DB::raw('coalesce(sum(i.total_amount_paid),0) as received'),
DB::raw('coalesce(sum(i.invoice_amount),0) as expected'))
->groupBy('businesses.id', 'businesses.name')
->get();
This works but it doesn't return all the businesses. I need all business returned irrespective of whether there are invoices during the set date duration or not.
It only returns all businesses when the snippet below is not within the query builder and that way I cannot sort by date:
->where('i.created_at', '>', $from)
->where('i.created_at', '<', $to)