Get all the loaded relations for the instance.
public getRelations ( ) : array | ||
Résultat | array |
private function saveRelations() { /** @var Eloquent $related */ foreach ($this->original->getRelations() as $related) { if ($related->isDirty()) { if ($related->save() === false) { return false; } } } return true; }
/** * Get the instance as an array. * * @return array */ public function toArray() { $model = []; foreach ($this->model->attributesToArray() as $attribute => $value) { $model[$attribute] = $this->{$attribute}; if (is_object($model[$attribute]) && is_callable([$model[$attribute], 'toArray'])) { $model[$attribute] = $model[$attribute]->toArray(); } } foreach ($this->model->getRelations() as $relationName => $relation) { try { $pivotRelationName = $relation->relationName; } catch (Exception $e) { // Fall though... } if (isset($pivotRelationName)) { $relationName = $pivotRelationName; } if (is_object($relation) && is_callable([$relation, 'toArray'])) { $model[$relationName] = $relation->toArray(); } else { $model[$relationName] = $relation; } } if (!empty($this->additionalAttributes)) { foreach ($this->additionalAttributes as $attribute) { $model[$attribute] = $this->{$attribute}; if (is_object($model[$attribute]) && is_callable([$model[$attribute], 'toArray'])) { $model[$attribute] = $model[$attribute]->toArray(); } } } return $model; }
/** * Get an array representing the properties of a model. * * @param \Illuminate\Database\Eloquent\Model $model * @return array */ public static function castModel(Model $model) { $attributes = array_merge($model->getAttributes(), $model->getRelations()); $visible = array_flip($model->getVisible() ?: array_diff(array_keys($attributes), $model->getHidden())); $results = []; foreach (array_intersect_key($attributes, $visible) as $key => $value) { $results[(isset($visible[$key]) ? Caster::PREFIX_VIRTUAL : Caster::PREFIX_PROTECTED) . $key] = $value; } return $results; }
/** * Decorate the relationships of an Eloquent object. * * @param \Illuminate\Database\Eloquent\Model $atom * * @return \Illuminate\Database\Eloquent\Model */ protected function decorateRelations(Model $atom) { foreach ($atom->getRelations() as $relationName => $model) { if ($model instanceof Collection) { $model = $this->createDecorator('Collection')->decorate($model); $atom->setRelation($relationName, $model); } else { $atom->setRelation($relationName, $this->decorate($model)); } } return $atom; }
/** * Decorate an Eloquent models relations. * * @param \Illuminate\Database\Eloquent\Model $model * * @return void */ protected function decorateModelRelations(Model $model) { if ($relations = $model->getRelations()) { foreach ($relations as $key => $value) { $model->setRelation($key, $this->decorate($value)); } } }
/** * Creates Links to all of the provided Model's relations * * Keep in mind that relations may only be scanned after they are attached to a certain Model, which means "join" * operation(s) where triggered. For example: with() method was called on the Model with params! * * @param Model $ent The Model that provides the Relations for our links * @param bool $forceReturn If set to true, it will return an empty array, and won't throw an Exception * @throws \InvalidArgumentException * @return array */ public function linksToEntityRelations(Model $ent, $forceReturn = false) { $links = []; $relations = $ent->getRelations(); if (!is_array($relations) || count($relations) == 0) { if ($forceReturn === false) { throw new \InvalidArgumentException('Provided Entity does not contain any relations, can\'t generate links...'); } return $links; } foreach ($relations as $key => $rel) { if ($rel instanceof ResourceEntity) { $links[] = $this->createLink($rel->getRootRelName(), $this->request->url() . '/' . $rel->getRootRelName()); } else { $links[] = $this->createLink($key, $this->request->url() . '/' . $key); } } return $links; }
/** * Resolve eager loaded relations from the model. * * @param \Illuminate\Database\Eloquent\Model $model * @return array */ protected function resolveRelations(Model $model) : array { return array_keys($model->getRelations()); }