Laravel

Laravel Eloquent: Accessors and Mutators

January 16, 2026 1 min read 48 views

Transform data with accessors and mutators:

Modern Accessor

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

Mutator

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

Combined

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

Related Posts

Comments (0)

Please log in to leave a comment. Log in

No comments yet. Be the first to comment!