4

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();
mpalencia
  • 5,481
  • 4
  • 45
  • 59

2 Answers2

0

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;

Ramin eghbalian
  • 2,348
  • 1
  • 16
  • 36
joy
  • 164
  • 1
  • 7
0

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

Bishal Rana
  • 21
  • 1
  • 5