vendredi 9 novembre 2018

Get most recent message sent to user per user

I am trying to sort my messages from users in the same way twitter handles it. So each user who has sent a message to me should be listed and with their latest message to me below.

My query to try and get this seems like it should work to me but it doesn't. It returns the first message the user sent to me so the opposite.

This is my query:

$messages = Message::with('user')->where('target_user_id', Auth::user()->id)
->latest()->groupBy('user_id')->get();

The Message table looks like this:

id
message
user_id ( user sending the message id)
target_user_id ( user the message is sent to id)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire