Laravel accessors and mutators for attribute handling

Carlos Mendez Jan 2026
3 tabs
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;

class User extends Model
{
    protected $fillable = ['first_name', 'last_name', 'email', 'password'];

    protected $hidden = ['password'];

    protected $appends = ['full_name', 'initials'];

    // Modern accessor/mutator using Attribute
    protected function password(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => $value, // No transformation on get
            set: fn ($value) => Hash::make($value), // Hash on set
        );
    }

    protected function email(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => strtolower($value),
            set: fn ($value) => strtolower($value),
        );
    }

    // Computed accessor
    protected function fullName(): Attribute
    {
        return Attribute::make(
            get: fn () => "{$this->first_name} {$this->last_name}",
        );
    }

    protected function initials(): Attribute
    {
        return Attribute::make(
            get: fn () => strtoupper(
                substr($this->first_name, 0, 1) .
                substr($this->last_name, 0, 1)
            ),
        );
    }

    // Cast attributes
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'preferences' => 'array',
            'is_admin' => 'boolean',
        ];
    }
}
3 files · php Explain with highlit

Accessors and mutators transform model attributes when retrieving or setting values. Accessors format data for presentation—converting cents to dollars, concatenating names, or generating computed properties. Mutators normalize input—hashing passwords, sanitizing HTML, or formatting phone numbers. I use attribute casting for automatic type conversion—dates to Carbon, JSON to arrays, encrypted strings. The modern approach uses single methods with Attribute return types combining get/set logic. Accessors never appear in database queries or toArray() unless appended. Mutators run before saving, ensuring data consistency. This pattern keeps data transformation logic centralized in models rather than scattered across controllers.