public function findConversations(array $user_ids, array $arguments = array(), $limit = 1) { //TODO: Do this using eloquent if (!is_array($user_ids) || count($user_ids) < 2) { throw new \Exception('You need at least 2 users for a conversation', 1410783987); } //Get all conversations of the first user and check if //the other users are in these conversations too. $userClass = EloquentBase::userClass(); $firstUser = $userClass::find($user_ids[0]); $conversations = $firstUser->conversations()->with('users')->get(); $filteredConversations = array(); foreach ($conversations as $conversation) { if (count($conversation->users) != count($user_ids)) { return false; } $argumentsFitting = true; foreach ($arguments as $column => $value) { if ($conversation->{$column} != $value) { $argumentsFitting = false; break; } } if (!$argumentsFitting) { continue; } $usersFitting = true; foreach ($conversation->users as $convUser) { if (!in_array($convUser->id, $user_ids)) { $usersFitting = false; break; } } if (!$usersFitting) { continue; } $filteredConversations[] = $conversation; if (count($filteredConversations) == $limit) { break; } } return $filteredConversations; }
/** * The users that are talking to each other * * @return Collection */ public function users() { $userClass = EloquentBase::userClass(); return $this->belongsToMany($userClass, parent::tableName('conversation_user'))->withTimestamps(); }
public function user() { $userClass = EloquentBase::userClass(); return $this->belongsTo($userClass); }