Write request means Laravel. Can't help with the method of GROUP BY.
Here is the MySQL query that works fine in the console:
SELECT users.*, sum(payments.amount) as sum_p
FROM users as users
LEFT JOIN payments as payments ON payments.user_id=users.id
WHERE payments.amount>0 AND payments.to_user_id=1 AND payments.status='success'
GROUP BY users.ORDER BY id DESC sum_p;
But the version with the interpreter:
User::selectRaw('users.*, SUM(payments.amount) as sum_p')
->join("payments", 'payments.user_id', 'users.id')
->where('payments.amount', '>', '0')
Which gives error Syntax error or access violation: 1055 'laravel_project.users.name' isn't in GROUP BY
But if selectRaw users.*
change to user.id
(as in groupBy), everything is working fine, but he will, of course, I only two columns: users.id and sum_p, not the User model completely. How to get around that? What I don't understand?