lundi 7 août 2017

How to get the last item of each grouped Items from database in Laravel 5

I am trying to get a list of messages stored in database and grouping them so that i could find only one message of each users. I have successfully retrieved the data with group, however the problem is I want the latest entry of each grouped message, but I am getting the first item instead. My database structure is

id-----from-----to-----msg
1      16       1      Hi
2      5        1      Namaste
3      6        1      Hola
4      6        1      Como Estas?
5      5        1      K cha?

what I need is

id-----from-----to-----msg
1      16       1      Hi
4      6        1      Como Estas?
5      5        1      K cha?

instead I'm getting

id-----from-----to-----msg
1      16       1      Hi
2      5        1      Namaste
3      6        1      Hola

my query is

$data['rows'] = Message::where('messages.to', $user->id)
                    ->orderBy('messages.id', 'desc')
                    ->join('users', 'messages.from', '=', 'users.id')
                    ->select('messages.*', 'users.fullname', 'users.id as user_id')
                    ->groupBy('messages.from')
                    ->get();

where $user->id = 1



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2vbsX2J
via IFTTT

Aucun commentaire:

Enregistrer un commentaire