vendredi 20 janvier 2017

Group Conversation by latest message Laravel

I cant seem to figure this out since mysql is not my strong suit. I've done a bit of research here but can't put two and two together, so i'm turning to the community for some help.

I am building a chat function inside my laravel app where two users can talk to each other. I cant figure out how to build the inbox (group the conversations together) portion of things. I've figured out how to get lastMessage grouped by sending it.

The following is inside my User Model:

public function lastMessages() { return $this->hasMany('App\Message', 'recipient_id') ->select('*') ->join(DB::raw('(Select max(id) as id from messages group by author_id) LatestMessage'), function($join) { $join->on('messages.id', '=', 'LatestMessage.id'); }) ->orderBy('created_at', 'desc'); }

My messages table consists of the following: author_id, recipient_id, messaged, viewed.

What I need to do is group messages, whether incoming our outgoing and display the latest message as inbox entry and once I click on that messages, the rest of the conversation pops up on the page.

My current sql shown above only gives me 1 last message, not the whole conversation.

Thanks in advance for all the help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire