/** * Returns all threads with new messages * * @return array */ public function threadsWithNewMessages() { $threadsWithNewMessages = []; $participants = Participant::where('user_id', $this->id)->lists('last_read', 'thread_id'); if ($participants) { $threads = Thread::whereIn('id', array_keys($participants))->get(); foreach ($threads as $thread) { if ($thread->updated_at > $participants[$thread->id]) { $threadsWithNewMessages[] = $thread->id; } } } return $threadsWithNewMessages; }
/** * Adds users to this thread * * @param array $participants list of all participants * @return void */ public function addParticipants(array $participants) { if (count($participants)) { foreach ($participants as $user_id) { Participant::firstOrCreate(['user_id' => $user_id, 'thread_id' => $this->id]); } } }