8000 Attribute accessors not working when accessor method is one word · Issue #2363 · mongodb/laravel-mongodb · GitHub
[go: up one dir, main page]

Skip to content

Attribute accessors not working when accessor method is one word #2363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
CraigMonva opened this issue Mar 10, 2022 · 2 comments
Open

Attribute accessors not working when accessor method is one word #2363

CraigMonva opened this issue Mar 10, 2022 · 2 comments

Comments

@CraigMonva
Copy link
  • 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

@mrneatly
Copy link
Contributor

Any progress here? I've came across the same issue. Been investigating this for hours as the cause of the problem was not obvious at all.

@mrneatly
Copy link
Contributor

Made a simple fix with help of @CraigMonva's hint. Let's hope it'll be reviewed soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0