mercredi 6 novembre 2019

laravel group by fetch latest record from table

I have one table where I want to retrieve data group by the user but I want the latest entry in result how can I do that by using eloquent.

here is an eloquent query I am using.

Product::whereNotNull('user_id')
         ->orderBy('id','desc')
         ->groupBy('user_id')
         ->get();

here is my table

Id   Name   user_id
-------------------------
1    A       1
2    b       1
3    c       2
4    d       2
5    e       3
6    f       3

result my query is giving me

Id   Name   user_id
-------------------------
1    A       1
3    c       2
5    e       3

result i want

Id   Name   user_id
-------------------------
2    b       1
4    d       2
6    f       3


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire