I want to SUM rows in MySQL table then merge and update. I'm trying to sum all the duplicate payments in a day. I will post my existing query but there are some limitations.
Example:
+----+------------+-------------+-------------+---------+
| id | date | merchant_id | investor_id | payment |
+----+------------+-------------+-------------+---------+
| 1 | 27/05/1989 | 4 | 7 | 1000 |
| 2 | 27/05/1989 | 4 | 7 | 1200 |
| 3 | 28/05/1989 | 4 | 7 | 1500 |
| 4 | 28/05/1989 | 4 | 7 | 1000 |
| 5 | 28/05/1989 | 5 | 8 | 1000 |
+----+------------+-------------+-------------+---------+
Expected result:
+----+------------+-------------+-------------+---------+
| id | date | merchant_id | investor_id | payment |
+----+------------+-------------+-------------+---------+
| 1 | 27/05/1989 | 4 | 7 | 2200 |
| 3 | 28/05/1989 | 4 | 7 | 2500 |
| 5 | 28/05/1989 | 5 | 8 | 1000 |
+----+------------+-------------+-------------+---------+
I tried this loop.
foreach ($existing_payments as $key => $payment)
{
ParticipentPayment::where('payment_date',$payment->payment_date) ->update(['payment' => \DB::raw("payment+$payment->payment"]);
}
Problem 1. I can only update one column at a time.
Problem 2. I'm programmatically looping through the rows and updating value, because of that if there is no duplicate value then the row not getting updated. I have to write another query to update. To delete existing query also.
Problem 3. It works well only for two payments.
Is there any possible solution in laravel eloquent merge(with sum) multiple rows and update?