8

I am trying to understand accessors & mutators and why I need them. And my another ask is the middle part of an attribute's method for an example:

Accessor:

public function getFirstNameAttribute($value)
{
   return ucfirst($value);
}

Mutator:

public function setFirstNameAttribute($value)
{
    $this->attributes['first_name'] = strtolower($value);
}

Here, we can see getFirstNameAttribute and setFirstNameAttribute methods and I haven't been able to clear the middle part FirstName of them. I will really be grateful for a better explanation and kind cooperation.

1
  • 1
    If you find this kind of question again, spin up empty project and try it yourself before taking time to ask on SO.
    – Kyslik
    Apr 22, 2018 at 20:42

3 Answers 3

20

Accessors create a "fake" attribute on the object which you can access as if it were a database column. So if your person has first_name and last_name attributes, you could write:

public function getFullNameAttribute()
{
  return $this->first_name . " " . $this->last_name;
}

Then you can call $user->full_name and it will return the accessor. It converts the function name into a snake_case attribute, so getFooBarBazAttribute function would be accessible through $user->foo_bar_baz.

Mutator is a way to change data when it is set, so if you want all your emails in your database to be lowercase only, you could do:

public function setEmailAttribute($value)
{
  $this->attributes['email'] = strtolower($value);
}

Then if you did $user->email = "EMAIL@GMAIL.com"; $user->save(); in the database it would set email@gmail.com

2
  • 2
    "as if it were a database column" = spot-on explanation. upvoted!
    – kapitan
    Jul 31, 2019 at 0:44
  • 2
    I don't think the statement a "fake" attribute is completely true. As you can call $model->name and the name attribute points to a valid table column and you still can have the getNameAttribute accessor. Accessors is a place where you want to mutate your data when they are asked for.
    – ssi-anik
    Aug 5, 2021 at 8:53
2

From the docs accessor and mutator both are public function in Laravel model for getting and setting model's attributes

An accessor will automatically be called by Eloquent when attempting to retrieve the value of the first_name attribute:

$fullName = $user->full_name;

It's for customizing a model's attributes or adding fake attributes

On the other hand mutator is for setting a real attribute of a model

Mutator will be automatically called when we attempt to set the value of the an attribute

0

Sometimes it happens that you have to modify the column value that was stored in a database. For Example, if you want to show column values that qualify some condition then you have to use Accessor and Mutator. where Accessor get value and Mutator set value.

By $append=[]; you can create any random column that fulfills your application demand. here I am only telling about Accessor. In this code total_holiday_hours is my DB column name and i want to modify that value so that i used $this->attributes['total_holiday_hours'] otherwise you can access this way $this->total_holiday_hours.

function getTotalHolidayHoursAttribute()
{
    if( $this->attributes['total_holiday_hours'] && $this->holiday_session == date('Y') )
    {
        return $this->attributes['total_holiday_hours'];
    }
    return 225;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy