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

mutateAttributeForArray() protected method

Get the value of an attribute using its mutator for array conversion.
protected mutateAttributeForArray ( string $key, mixed $value ) : mixed
$key string
$value mixed
return mixed
    protected function mutateAttributeForArray($key, $value)
    {
        $value = $this->mutateAttribute($key, $value);
        return $value instanceof Arrayable ? $value->toArray() : $value;
    }

Usage Example

Example #1
0
 /**
  * Override to make sure timestamp fields are mutated correctly
  *
  * @inheritdoc
  */
 protected function mutateAttributeForArray($key, $value)
 {
     // for timestamps, do NOT pass through the mutator, or
     // we'll end up with Carbon instances
     if (static::CREATED_AT === $key || static::UPDATED_AT === $key || in_array($key, $this->nonTimestampDates)) {
         return $value;
     }
     return parent::mutateAttributeForArray($key, $value);
 }
Model