Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Laravel

Laravel Eloquent: Accessors and Mutators

Transform attribute values automatically with accessors and mutators.

16 January 20261 min readLaravelاقرأ بالعربية

On this page

  1. Modern Accessor
  2. Mutator
  3. Combined

Transform data with accessors and mutators:

Modern Accessor

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

Mutator

php
protected function password(): Attribute
{
    return Attribute::make(
        set: fn ($value) => bcrypt($value),
    );
}

Combined

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

Keep reading

  • Laravel Action Classes Pattern16 January 20261 min read
  • Laravel Action Classes: Single Responsibility16 January 20261 min read
  • Laravel API Rate Limiting16 January 20261 min read