Laravel group by with select all column

Viewed 2711

Is there a way on laravel eloquent to query like this:

select users.* from users group by name

to implement it on code:

Users::select('name')->groupBy('name')->get();

Now I need to get other details (columns) of the user. Is there a way to query without aggregating every column on the model, this will not work:

Users::select('users.*')->groupBy('name')->get();
2 Answers

this is working;

Users::select('users.*')->groupBy('name')->get();

just make sure your model name. You don't have to write class name, and if model name is User then change to

User::groupBy('name')->get();

Don't forget to import the model at the top according to your folder structure use App\User;

Try this

Users::groupBy('name')->get();

Since laravel is independent of the type of database you use, so your query should be eloquent. For more details read this official documentation

Related