Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bmm's avatar
Level 6

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!

0 likes
16 replies
Tray2's avatar

What is the structure and data of you destinations table and what is the desired result?

bmm's avatar
Level 6

@Tray2

The desired result - I want to get rid of all duplicate tenantIDs from the destination table. That is SELECT all the rows and columns in destination table which has unique tenantID.

As mentioned above, this raw sql command is giving me the desired result. It is the matter of converting it to Laravel query builder.

select * from `destination` where userID=1 group by tenantID;
jlrdw's avatar

In database config turn strict mode to false.

bmm's avatar
Level 6

@jlrdw and @NoneNameDeveloper Tried both suggestions. It is still throwing the same error.

SQLSTATE[42000]: Syntax error or access violation: 1055 'database.destination.id' isn't in GROUP BY (SQL: select * from `destination` where `userID` = 1 group by `tenantID`)
NoneNameDeveloper's avatar

@bmm , Use like this, not Group By use Order By.


$destination = DB::table('destination')->where(array('user_id'=>'1' ))->orderBy('tenantID')->get();

This should work.
bmm's avatar
Level 6

@NoneNameDeveloper this will work but it won't serve my purpose. It will select all the data in the table with user_id=1. I want to omit the duplicate entries.

NoneNameDeveloper's avatar

@bmm , ok so:


$destination = DB::table(destination )
                 ->leftJoin('users', 'destination.user_id', '=', 1)
        ->select('destination.*', 'users.*')
                ->groupBy('tenantID')
                ->get();

This is the most optimal variant.
bmm's avatar
bmm
OP
Best Answer
Level 6

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]
2 likes
Tray2's avatar

You can also add the distinct keyword in your raw query.

SELECT DISTINCT * FROM table WHERE column1 = 1;
Cronix's avatar

You just have to explicitly select the column that you are using in the group_by. By default, laravel uses select *. You just need to say select *, tenantID. Then it will work with or without strict mode. It's better to use correct sql than turn strict mode off. That's like sweeping the problem under the rug and saying you "fixed" it.

->select('*, tenantID')
2 likes
Tray2's avatar

Agreed, more explicit is almost always better.

bmm's avatar
Level 6

@Cronix Unfortunately, it is throwing the same error with strict mode turned on. However, your suggestion will work if the strict mode is turned off.

2 likes
rubens2009's avatar

Simple solution(tested with Laravel 9 and Spatie/Permissions).

Controller:

//Get permissions group by guard name(3 in my case: web, admin and api)
$permissions = Permission::get()->groupBy('guard_name');
malik88888's avatar

->groupBy('guard_name'); must be after the get method not before it that worked for me: $tree = Tree::where('status', 'approved')->orderBy('id', 'DESC')->get()->groupBy('parentId');

Please or to participate in this conversation.