Eloquent: Mutators

Accessors and mutators allow you to format Eloquent attribute values when you retrieve or set them on model instances. For example, you may want to use the to encrypt a value while it is stored in the database, and then automatically decrypt the attribute when you access it on an Eloquent model.

In addition to custom accessors and mutators, Eloquent can also automatically cast date fields to Carbon instances or even .

To define an accessor, create a method on your model where Foo is the "studly" cased name of the column you wish to access. In this example, we'll define an accessor for the first_name attribute. The accessor will automatically be called by Eloquent when attempting to retrieve the value of the first_name attribute:

As you can see, the original value of the column is passed to the accessor, allowing you to manipulate and return the value. To access the value of the accessor, you may simply access the first_name attribute on a model instance:

  1. $user = App\User::find(1);
  2. $firstName = $user->first_name;

To define a mutator, define a setFooAttribute method on your model where Foo is the "studly" cased name of the column you wish to access. So, again, let's define a mutator for the first_name attribute. This mutator will be automatically called when we attempt to set the value of the first_name attribute on the model:

  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class User extends Model
  5. {
  6. /**
  7. * Set the user's first name.
  8. *
  9. * @param string $value
  10. */
  11. public function setFirstNameAttribute($value)
  12. {
  13. $this->attributes['first_name'] = strtolower($value);
  14. }
  15. }

The mutator will receive the value that is being set on the attribute, allowing you to manipulate the value and set the manipulated value on the Eloquent model's internal $attributes property. So, for example, if we attempt to set the first_name attribute to Sally:

  1. $user = App\User::find(1);
  2. $user->first_name = 'Sally';

In this example, the setFirstNameAttribute function will be called with the value Sally. The mutator will then apply the strtolower function to the name and set its resulting value in the internal $attributes array.

By default, Eloquent will convert the created_at and updated_at columns to instances of , which extends the PHP class to provide an assortment of helpful methods. You may customize which dates are automatically mutated, and even completely disable this mutation, by overriding the $dates property of your model:

When a column is considered a date, you may set its value to a UNIX timestamp, date string (Y-m-d), date-time string, and of course a DateTime / Carbon instance, and the date's value will automatically be correctly stored in your database:

  1. $user = App\User::find(1);
  2. $user->deleted_at = Carbon::now();
  3. $user->save();
  1. $user = App\User::find(1);
  2. return $user->deleted_at->getTimestamp();

Date Formats

By default, timestamps are formatted as 'Y-m-d H:i:s'. If you need to customize the timestamp format, set the $dateFormat property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON:

  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Flight extends Model
  5. {
  6. /**
  7. * The storage format of the model's date columns.
  8. *
  9. * @var string
  10. */
  11. }

The $casts property on your model provides a convenient method of converting attributes to common data types. The $casts property should be an array where the key is the name of the attribute being cast and the value is the type you wish to cast the column to. The supported cast types are: integer, real, float, double, string, boolean, object, array, collection, date, , and timestamp.

For example, let's cast the is_admin attribute, which is stored in our database as an integer (0 or 1) to a boolean value:

Now the is_admin attribute will always be cast to a boolean when you access it, even if the underlying value is stored in the database as an integer:

  1. $user = App\User::find(1);
  2. if ($user->is_admin) {
  3. //
  4. }

  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class User extends Model
  5. {
  6. /**
  7. * The attributes that should be casted to native types.
  8. *
  9. * @var array
  10. */
  11. protected $casts = [
  12. 'options' => 'array',
  13. ];
  14. }

Once the cast is defined, you may access the options attribute and it will automatically be deserialized from JSON into a PHP array. When you set the value of the options attribute, the given array will automatically be serialized back into JSON for storage:

  1. $user = App\User::find(1);
  2. $options = $user->options;
  3. $options['key'] = 'value';
  4. $user->options = $options;
  5. $user->save();