Accessors & Mutators

Transform attribute values on get and set

Accessors and mutators let you transform Eloquent attribute values when reading or writing them.

Database value
first_name: "john"
Accessor: ucfirst()
Displayed value
first_name: "John"
protected function firstName(): Attribute
{
  return Attribute::make(
    get: fn (string $value) => ucfirst($value),
  );
}

Transform attribute values when you read them from the model.