Accessors and Mutators in Laravel — a practical guide to Laravel accessors mutators with clear examples you can reuse in real projects.
Laravel Tutorial Series (13/27). Prefer one page? Read the complete Laravel tutorial (all 27 topics).
Short description
Accessors format values when you read them. Mutators transform values before they are saved.
Modern Attribute API (Laravel 9+)
use IlluminateDatabaseEloquentCastsAttribute;
protected function title(): Attribute
{
return Attribute::make(
get: fn (string $value) => ucfirst($value),
set: fn (string $value) => strtolower($value),
);
}
Example usage
$post->title = 'HELLO WORLD'; // stored as "hello world"
echo $post->title; // reads as "Hello world"
Appended accessor (not a DB column)
protected $appends = ['full_name'];
protected function fullName(): Attribute
{
return Attribute::get(
fn () => trim($this->first_name . ' ' . $this->last_name)
);
}