What are Mutators and Accessors in Laravel
Asked Answered
T

3

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.

Teri answered 22/4, 2018 at 19:24 Comment(1)
If you find this kind of question again, spin up empty project and try it yourself before taking time to ask on SO.Bottrop
A
21

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 protected]"; $user->save(); in the database it would set [email protected]

Absenteeism answered 22/4, 2018 at 19:37 Comment(2)
"as if it were a database column" = spot-on explanation. upvoted!Charteris
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.Yazzie
H
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

Hydroxy answered 22/4, 2018 at 19:41 Comment(0)
A
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;

}
Archy answered 6/6, 2022 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.