Skip to content

[13.x] Fix recycled models leaking into later calls on the same factory - #61884

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
xurshudyan:factory-recycle-leak
Oct 6, 2026
Merged

taylorotwell merged 1 commit into
laravel:13.xfrom
xurshudyan:factory-recycle-leak

Conversation

@xurshudyan

Copy link
Copy Markdown
Contributor

When a factory with has() or hasAttached() is reused, recycle() changes the shared relationship object instead of a copy, so the recycled models stick around for every later call:

$users = User::factory()->has(Post::factory()->count(3));

$users->recycle($teamA)->create();
$users->recycle($teamB)->create(); // posts randomly get team A or team B
$users->create();                  // posts still get team A or team B

This makes recycle() on the relationship return a copy, like everything else on the factory does.

@taylorotwell
taylorotwell merged commit 9e74d81 into laravel:13.x Oct 6, 2026
62 checks passed
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