Bad calculation with multiple left join and count (Laravel)
Budget: $10 – $30 USD
I have 3 tables in my database (users, payments and customers), what I want is to get all the new customers (of such date range) of a user and the sum of all payments (of such date range ). But the calculation is wrong, it only works fine if I remove the left join of customers, but I need to count the new customers, below the tables and my attempt:
Table users:
id | name
Table payments
id | users_id | price | date ....
Table customers
id | users_id | name | cuil | created_at ....
My try:
$query = Payment::query();
->leftJoin('users', 'users.id', '=', 'payments.users_id')
->leftJoin('customers', 'customers.users_id', '=', 'payments.users_id')
->whereBetween('payments.date', [$request->value1 . " 00:00:00",$request->value5 . " 11:59:59"])
->whereBetween('customers.created_at', [$request->value1 . " 00:00:00",$request->value5 . " 11:59:59"])
->select(\DB::raw('SUM(payments.price) as total, count(customers.id) as total_customers))
->groupBy('payments.users_id');
return $query->paginate(env('ITEMS_PER_PAGE', 10));
As a result I need the sum of clients in such date range and the sum of payments of such date range, but grouped by users. However, the calculation is wrong ...
Table users:
id | name
Table payments
id | users_id | price | date ....
Table customers
id | users_id | name | cuil | created_at ....
My try:
$query = Payment::query();
->leftJoin('users', 'users.id', '=', 'payments.users_id')
->leftJoin('customers', 'customers.users_id', '=', 'payments.users_id')
->whereBetween('payments.date', [$request->value1 . " 00:00:00",$request->value5 . " 11:59:59"])
->whereBetween('customers.created_at', [$request->value1 . " 00:00:00",$request->value5 . " 11:59:59"])
->select(\DB::raw('SUM(payments.price) as total, count(customers.id) as total_customers))
->groupBy('payments.users_id');
return $query->paginate(env('ITEMS_PER_PAGE', 10));
As a result I need the sum of clients in such date range and the sum of payments of such date range, but grouped by users. However, the calculation is wrong ...