Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

robjbrain's avatar

Eloquent relationship clashing with property name

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?

0 likes
2 replies
jlrdw's avatar

Not just laravel, mysql, php as well. There are times when you cannot use a reserved word. But in your case, I think it's just a false positive from the ide.

I had an issue with a field name:

stored

a reserve word in mysql, but adding the ticks around field names worked.

Believe me if there is a problem, you will get an error, or you will notice a whole section of code isn't running. Depending on the error.

robjbrain's avatar

Thanks for the reply @jlrdw

While I haven't had an issue now, I might do in the future. Such as my example with using the clean() and dirty() Eloquent methods.

I don't think the fact that I haven't noticed an error yet is enough to say there never will be one.

I just did a little test and doing this in a controller is fine:

return $this->changes->count();

Because changes is an Eloquent collection coming from the changes() method.

But this inside the model:

public function testChanges()
    {
        return $this->changes->count();
    }

Throws an error "Call to a member function count() on array"

Because it's happening within the class itself it has access to the protected property $changes on the HasAttributes trait which is an array.

It seems strange to me that I can't find anyone else who has encountered this.

It seems there should probably be warnings thrown if your attribute clashes with a protected property.

Please or to participate in this conversation.