/** * Saves the HasMany relational data. If id exists * then it updates the record otherwise it will * create the record. * * @param Model $relatedModel Model class * @param HasMany $hasMany * @param $data * @param $relationName * * @throws \Exception */ protected function saveHasManyData($relatedModel, HasMany $hasMany, $data, $relationName) { if ($this->exists) { $models = []; $pk = $hasMany->getRelated()->primaryKey; foreach ($data as $d) { /** @var Model $model */ $model = $relatedModel::find(ArrayUtils::get($d, $pk)); if (!empty($model)) { $fk = $hasMany->getPlainForeignKey(); $fkId = ArrayUtils::get($d, $fk); if (null === $fkId) { //Foreign key field is null therefore delete the child record. $model->delete(); continue; } elseif (!empty($fkId) && $fkId !== $this->{$this->primaryKey} && null !== ($parent = static::find($fkId))) { //Foreign key field is set but the id belongs to a different parent than this parent. //There the child is adopted by the supplied parent id (foreign key). $relatedData = [$relationName => [$d]]; $parent->update($relatedData); continue; } else { $model->update($d); continue; } } else { $model = new $relatedModel($d); } $models[] = $model; } $hasMany->saveMany($models); } }
/** * Filter the title or body by keyword * * @param Relations\HasMany $query * @param string $keyword * * @return void */ public function filterTitleOrBody(Relations\HasMany $query, $keyword) { if (!empty($keyword)) { $query->where(function (Eloquent\Builder $query) use($keyword) { $query->where('title', 'like', '%' . $keyword . '%'); $query->orWhere('body', 'like', '%' . $keyword . '%'); }); } }
/** * 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; }
/** * Create a new has one or many relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $foreignKey * @param string $localKey * @param $keyColumn * @param $valueColumn */ public function __construct(Builder $query, Model $parent, $foreignKey, $localKey, $keyColumn, $valueColumn) { parent::__construct($query, $parent, $foreignKey, $localKey); $this->keyColumn = $keyColumn; $this->valueColumn = $valueColumn; self::$validator = app()->make(Factory::class); }
/** * Attach a model instance to the parent model. * * This adds the field_id value * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Database\Eloquent\Model */ public function save(Model $model) { if ($this->fieldId) { $model->setAttribute($this->fieldKey, $this->fieldId); } return parent::save($model); }
/** * Attach a model instance to the parent model and trigger revisions. * * @param \Illuminate\Database\Eloquent\Model $model * * @return \Illuminate\Database\Eloquent\Model */ public function save(Model $model) { $this->parent->preSaveChild($this->relation, $model); if ($model = parent::save($model)) { $this->parent->postSaveChild($this->relation, $model); } return $model; }
/** * Create a new has many relationship instance. * @return void */ public function __construct(Builder $query, Model $parent, $foreignKey, $localKey, $relationName = null) { $this->relationName = $relationName; parent::__construct($query, $parent, $foreignKey, $localKey); $this->addDefinedConstraints(); }
/** * Determines the pagination of children * * @param mixed $perPage * @param HasMany $children * @return mixed */ public function determineChildrenPagination($perPage, HasMany $children) { if ($perPage === false) { return $children; } return is_null($perPage) ? $children->get() : $children->paginate($perPage); }
/** * Add multiple objects through a HasMany relation * * @param \Illuminate\Database\Eloquent\Relations\HasMany $relation * @param \Illuminate\Support\Collection $objects * @return array|\Traversable */ protected function addHasManyAssociatedObject(HasMany $relation, SupportCollection $objects) { return $relation->saveMany($objects); }
/** * 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; }
/** * {@inheritdoc} * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $foreignKey * @param string $localKey * @param \rsanchez\Deep\Repository\RepositoryInterface $repository * @param string $repositoryMethod */ public function __construct(Builder $query, Model $parent, $foreignKey, $localKey, RepositoryInterface $repository, $repositoryMethod = 'find') { parent::__construct($query, $parent, $foreignKey, $localKey); $this->repository = $repository; $this->repositoryMethod = $repositoryMethod; }
/** * Set or get the morph map for polymorphic relations. * * @param array|null $map * @param bool $merge * @return array * @static */ public static function morphMap($map = null, $merge = true) { //Method inherited from \Illuminate\Database\Eloquent\Relations\Relation return \Illuminate\Database\Eloquent\Relations\HasMany::morphMap($map, $merge); }
/** * 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; }