- Laravel-mongodb Version: 3.9.0
- PHP Version: 8.1.3
- Database Driver & Version: irrelevant (not a database issue)
Description:
Attribute accessors not working when accessor method is one word when using laravel 9 Attributes convention: https://laravel.com/docs/9.x/eloquent-mutators#defining-an-accessor
From the docs:
This method name should correspond to the "camel case" representation of the true underlying model attribute / database column when applicable.
e.g
first_name attribute should have a firstName method that returns \Illuminate\Database\Eloquent\Casts\Attribute which is then accessible by $model->first_name
name attribute should have a name method that returns \Illuminate\Database\Eloquent\Casts\Attribute which is then accessible by $model->name
When extending laravel-mongodb Model class, If model has an attribute accessor method which is one word. i.e the name example from above, its being treated like a relationship.
I've done some logging and believe this line is the issue:
https://github.com/jenssegers/laravel-mongodb/blob/master/src/Eloquent/Model.php#L158
Potential fix is don't call getRelationValue() if the return type of the method is \Illuminate\Database\Eloquent\Casts\Attribute like what happens in base laravel model? https://github.com/laravel/framework/blob/9.x/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L572
Description:
Attribute accessors not working when accessor method is one word when using laravel 9 Attributes convention: https://laravel.com/docs/9.x/eloquent-mutators#defining-an-accessor
From the docs:
This method name should correspond to the "camel case" representation of the true underlying model attribute / database column when applicable.
e.g
first_nameattribute should have afirstNamemethod that returns\Illuminate\Database\Eloquent\Casts\Attributewhich is then accessible by$model->first_namenameattribute should have anamemethod that returns\Illuminate\Database\Eloquent\Casts\Attributewhich is then accessible by$model->nameWhen extending laravel-mongodb Model class, If model has an attribute accessor method which is one word. i.e the
nameexample from above, its being treated like a relationship.I've done some logging and believe this line is the issue:
https://github.com/jenssegers/laravel-mongodb/blob/master/src/Eloquent/Model.php#L158
Potential fix is don't call
getRelationValue()if the return type of the method is\Illuminate\Database\Eloquent\Casts\Attributelike what happens in base laravel model? https://github.com/laravel/framework/blob/9.x/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L572