Skip to content

[13.x] Fix cached Attribute accessors returning stale values after setting the attribute - #62035

Merged
taylorotwell merged 2 commits into
laravel:13.xfrom
sajjadhossainshohag:fix/attribute-cast-cache-after-set
Oct 15, 2026
Merged

taylorotwell merged 2 commits into
laravel:13.xfrom
sajjadhossainshohag:fix/attribute-cast-cache-after-set

Conversation

@sajjadhossainshohag

Copy link
Copy Markdown
Contributor

Problem

A get-only Attribute accessor that returns an object is cached, but setting the attribute doesn't clear that cache:

protected function price(): Attribute
{
    return Attribute::get(fn ($value) => new Money($value));
}

$product->price->amount; // 1000

$product->update(['price' => 2500]);

$product->price->amount; // 1000

So a resource returned right after update() shows the old value until the model is refreshed. Accessors with a setter aren't affected, since they already update the cache.

Solution

Clear the cached value at the end of setAttribute(), the same way #61962 does for JSON path updates:

$product->update(['price' => 2500]);

$product->price->amount; // 2500

@taylorotwell
taylorotwell merged commit bff5d2a into laravel:13.x Oct 15, 2026
64 checks passed
@sajjadhossainshohag
sajjadhossainshohag deleted the fix/attribute-cast-cache-after-set branch October 15, 2026 14:41
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.

2 participants