Illuminate\Database\Eloquent\Model::relationsToArray PHP Method

relationsToArray() public method

Get the model's relationships in array form.
public relationsToArray ( ) : array
return array
    public function relationsToArray()
    {
        $attributes = [];
        foreach ($this->getArrayableRelations() as $key => $value) {
            // If the values implements the Arrayable interface we can just call this
            // toArray method on the instances which will convert both models and
            // collections to their proper array form and we'll set the values.
            if ($value instanceof Arrayable) {
                $relation = $value->toArray();
            } elseif (is_null($value)) {
                $relation = $value;
            }
            // If the relationships snake-casing is enabled, we will snake case this
            // key so that the relation attribute is snake cased in this returned
            // array to the developers, making this consistent with attributes.
            if (static::$snakeAttributes) {
                $key = Str::snake($key);
            }
            // If the relation value has been set, we will set it on this attributes
            // list for returning. If it was not arrayable or null, we'll not set
            // the value on the array because it is some type of invalid value.
            if (isset($relation) || is_null($value)) {
                $attributes[$key] = $relation;
            }
            unset($relation);
        }
        return $attributes;
    }

Usage Example

 /**
  * Lets the model consume the old one
  * @param  Model  $model The old model
  * @return $this
  */
 public function consumeModel(Model $model)
 {
     $this->consumedModel = $model;
     $this->consumedModelProps = array_merge($model->attributesToArray(), $model->relationsToArray());
     return $this;
 }
Model