Skip to main content

Introduction

Accessors, mutators, and attribute casting let you transform Eloquent attribute values when you retrieve or set them on model instances.
  • Accessor — transforms a raw database value before it reaches your application
  • Mutator — transforms a value set by your application before it is stored in the database
  • Cast — declaratively defines type conversion without writing accessor/mutator methods

Defining an accessor

Create a protected method on your model whose name corresponds to the attribute in camelCase. The return type must be Illuminate\Database\Eloquent\Casts\Attribute.
The get closure receives the raw column value. Access the accessor via the snake_case property name.
To include computed accessor values in JSON or array output, add them to the model’s $appends property.

Building value objects from multiple attributes

The get closure accepts a second $attributes argument — an array of all current model attributes. Use it to build a value object from multiple columns.

Accessor caching

Eloquent automatically caches value objects returned from accessors so the same instance is returned on repeated access. To cache primitive values as well, call shouldCache().
To disable object caching, call withoutObjectCaching().

Defining a mutator

Add a set argument to Attribute::make(). Accessor and mutator can be defined in the same method.
Setting the attribute on the model triggers the set closure.

Mutating multiple attributes

Return an array from the set closure to update multiple database columns at once.

Attribute casting

Casts let you declare type conversions without writing accessor/mutator methods. Return an array from the model’s casts() method.

Built-in cast types

Attributes that are null will not be cast. Never define a cast (or an attribute) that has the same name as a relationship, and do not cast the model’s primary key.

Stringable casting

Use AsStringable to cast an attribute to a fluent Illuminate\Support\Stringable object.

Array and JSON casting

The array cast automatically deserializes JSON columns to PHP arrays on retrieval and serializes them back on save.
Use the -> operator to update a single JSON key.

AsArrayObject and AsCollection

The standard array cast returns a primitive type — directly modifying an offset triggers a PHP error. Use AsArrayObject or AsCollection to avoid this.
Provide a custom collection class via using().

Date casting

created_at and updated_at are cast to Carbon by default. Cast additional date columns the same way.
Specify a format to control JSON serialization output.
Override serializeDate() to apply a default format to all dates (does not affect database storage format).
Use immutable_datetime to receive a CarbonImmutable instance instead of Carbon. Operations on immutable objects return new instances, preventing accidental mutation of shared state.

Enum casting

Cast attributes to PHP 8.1+ Backed Enums.
The backing value is stored in the database; retrieval returns the Enum instance.

Casting arrays of enums

Store multiple enum values in a single column using AsEnumCollection.

Query time casting

Apply casts dynamically during a query with withCasts().

Custom casts

You can create your own cast classes by implementing the CastsAttributes interface with get and set methods.
For full details on custom casts — including value object patterns, inbound-only casts, and Castables — see the advanced page below.

Custom casts deep dive

Learn how to implement CastsAttributes, use Value Object patterns, inbound casting, and Castables.

Eloquent API resources

Transform Eloquent models into consistent JSON API responses using resource classes.
Last modified on April 12, 2026