Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Tim-KH's avatar

How do Accessor and Mutator work

So I have integrated some Accessors and Mutators in my latest project but honestly I dont know HOW they work. I understand what they do and when to use them but how does laravel know what I want to do.

In my view I can simply call {{$user->image}} and that will trigger my Mutator "getImageAttribute" without having to call the acuall method or anything - How come?

I just would like to know how this feature works in general so I get a general idea what is going on here since it has helped my greatly but I want to understand the "native" aspect of it a little more. And maybe integrate in in non laravel projects.

1 like
3 replies
bobbybouwmann's avatar
Level 88

Well Laravel uses the magic __get method for this that is build into PHP. From there it tries to check if they property exists on the model. If it doesn't exists it keeps searching for other possibilities like relations and accessors.

But specifically for the accessor you can find the code here: https://github.com/laravel/framework/blob/5.8/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L302 If you look closely you see the following code:

// Model.php

public function __get($key)
{
    return $this->getAttribute($key);
}

public function getAttribute($key)
{       
    // Check if the attribute exists or check if there is a method for the getter
    // So for key "image" we basically check in the "hasGetMutator" method if 
    // the method "getImageAttribute" exists on the current class.
    if (array_key_exists($key, $this->attributes) ||$this->hasGetMutator($key)) {
        // Get the actual attribute
        return $this->getAttributeValue($key);
    }
}

public function getAttributeValue($key)
{
    // Get the original value from the model based on the key
    $value = $this->getAttributeFromArray($key);

    // If the attribute has a get mutator, we will call that then return what
    // it returns as the value, which is useful for transforming values on
// retrieval from the model to a form that is more useful for usage.
    
    // "hasGetMutator" does the same check as before
    if ($this->hasGetMutator($key)) {
        // Get the actual mutated value, we also pass in the original value so we can mutate that
        return $this->mutateAttribute($key, $value);
}
}

protected function mutateAttribute($key, $value)
{
    // Basically call the method on the class
    // In this case "getImageAttribute($value);
    return $this->{'get'.Str::studly($key).'Attribute'}($value);
}

Note: I stripped some code to make it more clear!

For mutators it kinda works the same way! Instead of using the magic __get it uses the magic __set method from PHP. There is also a similair funtion for that

public function __set($key, $value)
{
    $this->setAttribute($key, $value);
}

public function setAttribute($key, $value)
{
    if ($this->hasSetMutator($key)) {
        return $this->setMutatedAttributeValue($key, $value);
    }
}

public function hasSetMutator($key)
{
    return method_exists($this, 'set'.Str::studly($key).'Attribute');
}

protected function setMutatedAttributeValue($key, $value)
{
    return $this->{'set'.Str::studly($key).'Attribute'}($value);
}

Let me know if this makes sense to you!

3 likes
Tim-KH's avatar

@BOBBYBOUWMANN - That makes perfect sense. I was afraid that it will be to complicated to explain on the forums but its quite self explanatory once you read it a bit.

Thank you for posting the methods used.

Please or to participate in this conversation.