setRelation() public method

Set the specific relationship in the model.
public setRelation ( string $relation, mixed $value )
$relation string
$value mixed
Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * Build the view model representations of the relations.
  *
  * @return void
  */
 protected function buildRelations()
 {
     $relations = $this->model->getRelations();
     foreach ($relations as $relationName => $relation) {
         // TODO: handle MorphToMany
         //if ($relation instanceof MorphPivot) {
         //    dd($relation->getMorphClass(), $relation);
         //    dd(get_class_methods($relation));
         //}
         // For pivots, we'll test for the snake cased version of the class name as the
         // relation's name, and check if a view model class has been defined on this
         // view model's relations.
         if ($relation instanceof Pivot) {
             $originalRelationName = $relationName;
             $relationName = $this->relationName ?: snake_case(class_basename($relation));
             if (isset($this->relations[$relationName])) {
                 $relationViewModel = $this->relations[$relationName];
                 $relation = static::make($relation, $relationViewModel);
                 $this->model->setRelation($originalRelationName, $relation);
             }
         } elseif (isset($this->relations[$relationName])) {
             $relationViewModel = $this->relations[$relationName];
             $relation = static::make($relation, $relationViewModel);
             $this->model->setRelation($relationName, $relation);
         }
     }
 }
 /**
  * Get the pivot attribute from a model.
  *
  * @param  \Illuminate\Database\Eloquent\Model  $model
  * @param  \Illuminate\Database\Eloquent\Relations\Relation  $parentRelation
  */
 public static function loadPivotAttribute(Model $model, Relation $parentRelation = null)
 {
     $attributes = $model->getAttributes();
     foreach ($attributes as $key => $value) {
         if ($key === 'pivot') {
             unset($model[$key]);
             $pivot = $parentRelation->newExistingPivot($value);
             $model->setRelation($key, $pivot);
         }
     }
 }
Example #4
0
 /**
  * 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));
         }
     }
 }
Example #5
0
 /**
  * Set the specific relationship in the model.
  *
  * @param  string $relationName
  * @param  mixed $value
  * @param Relation|null $relation
  * @return $this
  */
 public function setRelation($relationName, $value, $relation = null)
 {
     if ($relation instanceof Relation) {
         static::$relationsCache[$this->getRelationCacheKey($relationName)] = $relation;
     }
     return parent::setRelation($relationName, $value);
 }
Example #6
0
 /**
  * Set value to relation.
  *
  * @param string $key
  */
 public function updateValue($key)
 {
     $this->entity->setRelation($key, $this->getValue($key));
 }