Adding Setters and Getters to Laravel Model

Adding Setters and Getters to Laravel Model

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Parent::__get and __set Should Not Define Your Accessors

Attempting to implement an interface on a Laravel Eloquent Model by manually overriding __get and __set using the parent class methods is a common pattern that often leads to subtle bugs. While your proposed approach of calling parent::__get('foo') inside a custom getter looks logical, it bypasses the core mechanisms that make Eloquent work. You are essentially creating a shadow layer over the magic method resolution without benefiting from the framework's caching or mass-assignment protections.

When you call $model->foo, Laravel does not execute a simple __get. It checks if an accessor exists using the getFooAttribute convention, then looks for mutators named setFooAttribute. If neither is found, it falls back to accessing the underlying attribute via $attributes['foo']. By defining public function getFoo(), you are creating a new PHP method. While this will technically return the value of the property if implemented correctly, it breaks the contract expected by other parts of your application that rely on Eloquent's magic methods for features like dirty checking or event broadcasting.

The specific line parent::__set('foo', $value) is particularly dangerous in a Model context. The parent class in Laravel (usually Illuminate\Database\Eloquent\Model) does not have public __get and __set methods that you can safely call to read/write raw attributes. Those magic methods are the interface between your PHP object and the $attributes array managed by Eloquent. Calling them directly from a custom method often leads to recursion issues or simply fails because the parent class in the standard Laravel installation does not expose these as callable functions for this purpose. In older versions of PHP or specific base classes, __set might write to the attributes array, but it bypasses the setAttribute logic where you would normally handle casting (e.g., converting dates to strings) or validation.

If your goal is strict adherence to an interface that requires named getters and setters, such as getFoo() and setFoo(), you must accept that you are stepping outside of standard Eloquent conventions. This means you lose the ability to use ->foo syntax while still supporting $model->getFoo(). To make this work without breaking existing code, you cannot simply call parent magic methods; you must interact directly with the $attributes array and ensure you handle dirty checking manually if you intend to persist changes.

Here is a safer implementation that respects the interface contract while avoiding the pitfalls of calling parent magic methods:

class MyClass extends Model implements SomeContract
{
    public function setFoo($value)
    {
        // Manually update the attributes array
        $this->attributes['foo'] = $value;
        
        // Mark the attribute as dirty to maintain Eloquent's change tracking
        $this->touchAttributes(); 
        return $this;
    }

    public function getFoo()
    {
        // Access the raw value directly from the attributes array
        return $this->attributes['foo'];
    }
}

This approach ensures that the attribute is always stored in the $attributes collection, which Eloquent monitors. If you were to use parent::__set, you risk creating a disconnect between your custom getter and the internal state tracking logic, potentially causing mass assignment exceptions or data loss during serialization.

If you require this pattern for external integrations or strict typing requirements, ensure that any code expecting $model->foo is updated to use the accessor method instead, or provide an alias using getFooAttribute if you want to maintain backward compatibility with the dot syntax. For more complex scenarios involving custom accessors and mutators that need to interact with interfaces, consider exploring resources on defining custom Eloquent behaviors at https://laravelcompany.com for best practices on extending model functionality without compromising the framework's integrity.

Our network