1 year ago

Eloquent relationship clashing with property name

Posted 1 year ago by robjbrain

I have a model called Change and a model called Submission and the Submission model has many Changes. I noticed when accessing $submission->changes my IDE flagged up that this was a protected property because the HasAttributes trait has the property $changes.

Does this mean there are certain keywords that we can't use for attributes and relationships in an Eloquent model?

Surprisingly my code has worked fine with a "changes" relationship so far, but I imagine there may be issues in some use cases, such as if an observer wanted to use the dirty() and clean() methods.

Has anyone had this issue before? Do you just have to rename the attribute/relationship? What happens if Laravel adds a new property to a trait in the future, do you have to refactor to not clash the names?

I'm surprised this hasn't come up more often. Has Laravel put something in place to prevent this being an issue?

Please sign in or create an account to participate in this conversation.