Nahid\Talk\Conversations\ConversationRepository::threadsAll PHP Метод

threadsAll() публичный Метод

* retrieve all message thread with latest one message and sender and receiver user model
public threadsAll ( $user, $offset, $take )
$user
$offset
$take
    public function threadsAll($user, $offset, $take)
    {
        $msgThread = Conversation::with(['messages' => function ($q) use($user) {
            return $q->latest();
        }, 'userone', 'usertwo'])->where('user_one', $user)->orWhere('user_two', $user)->offset($offset)->take($take)->get();
        $threads = [];
        foreach ($msgThread as $thread) {
            $conversationWith = $thread->userone->id == $user ? $thread->usertwo : $thread->userone;
            $message = $thread->messages->first();
            $message->user = $conversationWith;
            $threads[] = $message;
        }
        return collect($threads);
    }

Usage Example

Пример #1
0
 /**
  * fetch all inbox with soft deleted message for currently loggedin user with pagination.
  *
  * @param int $offset
  * @param int $take
  * @return array
  */
 public function getInboxAll($offset = 0, $take = 20)
 {
     return $this->conversation->threadsAll($this->authUserId, $offset, $take);
 }