Nahid\Talk\Conversations\ConversationRepository::threads PHP Method

threads() public method

* retrieve all message thread without soft deleted message with latest one message and sender and receiver user model
public threads ( $user, $offset, $take )
$user
$offset
$take
    public function threads($user, $offset, $take)
    {
        $conv = new Conversation();
        $conv->authUser = $user;
        $msgThread = $conv->with(['messages' => function ($q) use($user) {
            return $q->where(function ($q) use($user) {
                $q->where('user_id', $user)->where('deleted_from_sender', 0);
            })->orWhere(function ($q) use($user) {
                $q->where('user_id', '!=', $user);
                $q->where('deleted_from_receiver', 0);
            })->latest();
        }, 'userone', 'usertwo'])->where('user_one', $user)->orWhere('user_two', $user)->offset($offset)->take($take)->get();
        $threads = [];
        foreach ($msgThread as $thread) {
            $collection = (object) null;
            $conversationWith = $thread->userone->id == $user ? $thread->usertwo : $thread->userone;
            $collection->thread = $thread->messages->first();
            $collection->withUser = $conversationWith;
            $threads[] = $collection;
        }
        return collect($threads);
    }

Same methods

ConversationRepository::threads ( $user, $order, $offset, $take )

Usage Example

Beispiel #1
0
 /**
  * fetch all inbox for currently loggedin user with pagination.
  *
  * @param int $offset
  * @param int $take
  * @return array
  */
 public function getInbox($offset = 0, $take = 20)
 {
     return $this->conversation->threads($this->authUserId, $offset, $take);
 }