What is the structure and data of you destinations table and what is the desired result?
Select all and group by using a column value
Hi guys,
I know it is a simple question but couldn't find out a proper Laravel way to achieve this. Can you please convert the below sql query to Laravel:-
select * from `destination` where userID=1 group by tenantID;
I've done this :
$destination = Destination::groupBy('tenantID')->where('userID', 1)->get();
And it is throwing me the below error:
SQLSTATE[42000]: Syntax error or access violation: 1055 'database.destination.id' isn't in GROUP BY
However, the below will work successfully but it will select only the tenantID column(of course it will select only that column). As mentioned before, I need all the column details.
$destination = Destination::select('tenantID')->groupBy('tenantID')->where('userID', 1)->get();
Any help would be really appreciated!
Ok. Finally resolved it. For future reference, this error is caused due to the strict mode is enabled as @jlrdw suggested. Thanks for the suggestion!
More details are here: https://github.com/laravel/framework/issues/14997
Basically, to resolve it. Turn the sql strict mode to false in MySQL and Laravel.
1. In MySQL - SET sql_mode = ''; or any mode other than STRICT_TRANS_TABLES
2. In Laravel - go to database.php and change
'mysql' => [ 'strict' => false]
Please or to participate in this conversation.