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

attributesToArray() public method

Convert the model's attributes to an array.
public attributesToArray ( ) : array
return array
    public function attributesToArray()
    {
        $attributes = $this->getArrayableAttributes();
        // If an attribute is a date, we will cast it to a string after converting it
        // to a DateTime / Carbon instance. This is so we will get some consistent
        // formatting while accessing attributes vs. arraying / JSONing a model.
        foreach ($this->getDates() as $key) {
            if (!isset($attributes[$key])) {
                continue;
            }
            $attributes[$key] = $this->serializeDate($this->asDateTime($attributes[$key]));
        }
        $mutatedAttributes = $this->getMutatedAttributes();
        // We want to spin through all the mutated attributes for this model and call
        // the mutator for the attribute. We cache off every mutated attributes so
        // we don't have to constantly check on attributes that actually change.
        foreach ($mutatedAttributes as $key) {
            if (!array_key_exists($key, $attributes)) {
                continue;
            }
            $attributes[$key] = $this->mutateAttributeForArray($key, $attributes[$key]);
        }
        // Next we will handle any casts that have been setup for this model and cast
        // the values to their appropriate type. If the attribute has a mutator we
        // will not perform the cast on those attributes to avoid any confusion.
        foreach ($this->getCasts() as $key => $value) {
            if (!array_key_exists($key, $attributes) || in_array($key, $mutatedAttributes)) {
                continue;
            }
            $attributes[$key] = $this->castAttribute($key, $attributes[$key]);
            if ($attributes[$key] && ($value === 'date' || $value === 'datetime')) {
                $attributes[$key] = $this->serializeDate($attributes[$key]);
            }
        }
        // Here we will grab all of the appended, calculated attributes to this model
        // as these attributes are not really in the attributes array, but are run
        // when we need to array or JSON the model for convenience to the coder.
        foreach ($this->getArrayableAppends() as $key) {
            $attributes[$key] = $this->mutateAttributeForArray($key, null);
        }
        return $attributes;
    }

Usage Example

Example #1
0
 /**
  * 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;
 }
All Usage Examples Of Illuminate\Database\Eloquent\Model::attributesToArray
Model