/** * Load pages user is allowed to access and set the collection as 'pages' relation * */ protected function loadPages() { $pages = Page::join('page_role as pr', 'pr.page_id', '=', 'pages.id')->join('role_user as ru', 'ru.role_id', '=', 'pr.role_id')->where('ru.user_id', $this->id)->distinct()->get(['pages.*', 'user_id']); $hasMany = new Illuminate\Database\Eloquent\Relations\HasMany(Page::query(), $this, 'user_id', 'id'); $hasMany->matchMany([$this], $pages, 'pages'); // If there is no collection to set the relation on, create a blank one if (!isset($this->pages)) { $this->setRelation('pages', new Collection()); } return $this; }
/** * Load pages user is allowed to access and set the collection as 'pages' relation * */ protected function loadPermissions() { $permissions = Permission::join('permission_role as pr', 'pr.permission_id', '=', 'permissions.id')->join('role_user as ru', 'ru.role_id', '=', 'pr.role_id')->where('ru.user_id', $this->id)->distinct()->get(['permissions.*', 'user_id']); $hasMany = new HasMany(Permission::query(), $this, 'user_id', 'id'); $hasMany->matchMany([$this], $permissions, 'permissions'); // If there is no collection to set the relation on, create a blank one if (!isset($this->permissions)) { $this->setRelation('permissions', new Collection()); } return $this; }
/** * Match the eagerly loaded results to their many parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array * @static */ public static function matchMany($models, $results, $relation) { //Method inherited from \Illuminate\Database\Eloquent\Relations\HasOneOrMany return \Illuminate\Database\Eloquent\Relations\HasMany::matchMany($models, $results, $relation); }
/** * Query the DB and load the HasMany relationship for commands. * * @return $this */ private function loadCommands() { $collection = Command::join('deploy_steps', 'commands.id', '=', 'deploy_steps.command_id')->where('deploy_steps.deployment_id', $this->getKey())->distinct()->orderBy('step')->orderBy('order')->get(['commands.*', 'deployment_id']); $hasMany = new HasMany(Command::query(), $this, 'deployment_id', 'id'); $hasMany->matchMany([$this], $collection, 'commands'); return $this; }