Skip to content

[13.x] Fix inconsistent accessor attribute name conversion#59325

Draft
JoshSalway wants to merge 3 commits intolaravel:13.xfrom
JoshSalway:fix/accessor-attribute-name
Draft

[13.x] Fix inconsistent accessor attribute name conversion#59325
JoshSalway wants to merge 3 commits intolaravel:13.xfrom
JoshSalway:fix/accessor-attribute-name

Conversation

@JoshSalway
Copy link
Copy Markdown

@JoshSalway JoshSalway commented Mar 22, 2026

Summary

Fixes inconsistent behavior when accessing Attribute-style accessors via alternate snake_case forms (e.g., foo_1_bar vs foo1_bar). Both forms correctly resolve to the same foo1Bar() method when getting values, but append('foo_1_bar')->toArray() would fail because the mutator cache only stored the canonical form foo1_bar.

Normalizes cache keys by round-tripping through camel→snake conversion so that alternate forms resolve to the same entry.

Performance

Two prior PRs addressing this issue (#54578, #54793) were rejected due to performance concerns -- Str::camel() + Str::snake() runs on every attribute access, which is a hot path.

This PR includes a fast-path short-circuit: the normalization is only needed when the attribute key contains a digit (the only case where foo_1_bar vs foo1_bar ambiguity exists). For the 99% common case (first_name, email, created_at, etc.), the key is returned unchanged with zero overhead beyond a single preg_match('/\d/', $key) check.

Test plan

  • All 222 existing Eloquent model tests pass
  • New test verifies both foo1_bar and foo_1_bar work with append() and toArray()

Fixes #54570

@github-actions
Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

Josh Salway and others added 3 commits March 28, 2026 14:58
…ution

When using the Attribute syntax (e.g. foo1Bar(): Attribute), multiple
snake_case forms like "foo_1_bar" and "foo1_bar" both map to the same
camelCase method "foo1Bar". However, only "foo1_bar" was stored in the
mutator cache (from Str::snake('foo1Bar')).

This caused append('foo_1_bar')->toArray() to fail with "Call to
undefined method getFoo1BarAttribute()" because the cache lookup for
"foo_1_bar" missed, and it fell through to the legacy accessor path.

The fix normalizes cache keys by round-tripping through camel->snake
conversion, ensuring that alternate snake_case forms resolve to the
same cache entry.

Fixes laravel#54570

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…gments

Verifies that both "foo1_bar" and "foo_1_bar" forms correctly resolve
to the same Attribute accessor when used with append() and toArray().

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Skip expensive Str::camel/Str::snake normalization for the 99%
of attribute keys that contain no digits, since the ambiguity
this method resolves (e.g. foo_1_bar vs foo1_bar) only exists
when digits are present.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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

Successfully merging this pull request may close these issues.

Inconsistent accessor attribute name conversion

1 participant