Answer the question
In order to leave comments, you need to log in
Why doesn't GROUP BY work in Laravel 5?
I am writing a query using Laravel. I can't handle the GROUP BY method.
Here is a 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.id ORDER BY sum_p DESC;
User::selectRaw('users.*, SUM(payments.amount) as sum_p')
->join("payments", 'payments.user_id', 'users.id')
->where('payments.status', 'success')
->where('payments.amount', '>', '0')
->where('payments.to_user_id', $user->id)
->groupBy('users.id')->orderBy('sum_p', 'desc')
->get();
Answer the question
In order to leave comments, you need to log in
File /config/database.php , line 53: 'strict' => true,
(in the "section" 'mysql'), change the value to false .
Details:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question