lundi 23 janvier 2017

Laravel Eloquent Chat Inbox Query

I keep trying to ask this question here on stackoverflow several times but cant seem to get any help. Maybe third time is the charm.

I'm building a chat feature inside laravel app and I'm struggling to get the inbox section right. I've read a tonn of other threads on here but mysql is not my strongest suit so I'm having an issue.

Basically, I got this query to get me the last message from the user who just messaged me to show up in my inbox. The problem is, if I'm the one sending out a message to another user, it will not display anything in my inbox since I'm the author not the recipient. How would I make this query to display the latest message from another user in my inbox and also show that I have started a conversation to another user.

 $chat_inbox = Message::where('author_id', $this->user->id)
        ->orWhere('recipient_id', $this->user->id)
            ->join(DB::raw('(Select max(id) as id from messages where author_id ='.$this->user->id.' group by unique_chat_id) LatestMessage'), function($join) {
                $join->on('messages.id', '=', 'LatestMessage.id');
            })
            ->get();

Mysql table columns are: unique_chat_id, author_id, recipient_id, viewed, user_read, created_at, and updated_at

If you need any further details, please let me know. Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire