Home > OS >  What is the equivalent of getAttribute with a parameter in Laravel 9
What is the equivalent of getAttribute with a parameter in Laravel 9

Time:07-17

I have a Laravel 7 project, that I'm converting to Laravel 9. I have a Model called Product with an accessor called getAttribute ( in my Laravel 7 project ) :

class Product extends from Model
{
    public function getAttribute($key)
    {
       return $key;
    }
}

What is the equivalent of getAttribute with the $key in Laravel 9 please. I saw the documentation but they give only an example by a column Name like FirstName. which is not my case because getAttribute is not a column

Thank you

CodePudding user response:

For one i would be concerned with the implementation, since you are discarding a lot of Laravel logic by overwriting that.

With that said, nothing has changed and it should work the same. I think you are mixing up Eloquent Getters, and this functionality that is a general logic to get properties off a model, as you can see in github in goes through a lot of boilerplate logic for the model property handling.

GetAttribute implementation

  • Related