public function scopeBetween(Builder $query, array $participants)
    {
        return $query->whereHas('participants', function (Builder $q) use ($participants) {
            $q->whereIn('user_id', $participants)
                ->select($this->getConnection()->raw('DISTINCT(thread_id)'))
                ->groupBy('thread_id')
                ->havingRaw('COUNT(thread_id)=' . count($participants));
        });
    }
public function scopeBetween(Builder $query, array $participants)
    {
        return $query->whereHas('participants', function (Builder $q) use ($participants) {
            $q->whereIn('user_id', $participants)
                ->select($this->getConnection()->raw('DISTINCT(thread_id)'))
                ->groupBy('thread_id')
                ->havingRaw('COUNT(thread_id)=' . count($participants));
        });
    }
public function scopeBetween ( Builder $ query , array $ participants ) { return $ query -> whereHas ( 'participants' , function ( Builder $ q ) use ( $ participants ) { $ q -> whereIn ( 'user_id' , $ participants ) -> select ( $ this -> getConnection ( ) -> raw ( 'DISTINCT(thread_id)' ) ) -> groupBy ( 'thread_id' ) -> havingRaw ( 'COUNT(thread_id)=' . count ( $ participants ) ) ; } ) ; }
Returns threads between given user ids.

@param \Illuminate\Database\Eloquent\Builder $query
@param array $participants

@return \Illuminate\Database\Eloquent\Builder
Returns threads between given user ids .