public function scopeForUserWithNewMessages($query, $userId)
    {
        return $query->join('participants', 'threads.id', '=', 'participants.thread_id')
            ->where('participants.user_id', $userId)
            ->where(function ($query) {
                $query->where('threads.updated_at', '>', $this->getConnection()->raw($this->getConnection()->getTablePrefix() . 'participants.last_read'))
                    ->orWhereNull('participants.last_read');
            })
            ->select('threads.*');
    }
public function scopeForUserWithNewMessages($query, $userId)
    {
        return $query->join('participants', 'threads.id', '=', 'participants.thread_id')
            ->where('participants.user_id', $userId)
            ->where(function ($query) {
                $query->where('threads.updated_at', '>', $this->getConnection()->raw($this->getConnection()->getTablePrefix() . 'participants.last_read'))
                    ->orWhereNull('participants.last_read');
            })
            ->select('threads.*');
    }
public function scopeForUserWithNewMessages ( $ query , $ userId ) { return $ query -> join ( 'participants' , 'threads.id' , '=' , 'participants.thread_id' ) -> where ( 'participants.user_id' , $ userId ) -> where ( function ( $ query ) { $ query -> where ( 'threads.updated_at' , '>' , $ this -> getConnection ( ) -> raw ( $ this -> getConnection ( ) -> getTablePrefix ( ) . 'participants.last_read' ) ) -> orWhereNull ( 'participants.last_read' ) ; } ) -> select ( 'threads.*' ) ; }
Returns threads with new messages that the user is associated with

@param $query
@param $userId
@return mixed
Returns threads with new messages that the user is associated with