Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Allow factories to recycle multiple models of a given type #44328

Merged
merged 3 commits into from
Oct 3, 2022

Conversation

michal3377
Copy link
Contributor

@michal3377 michal3377 commented Sep 27, 2022

This PR is an enhancement to recently added feature, Eloquent Factory recycling (#44107) and adds support for recycling multiple models of the same type.
Model recycling is a great feature, making factories much cleaner and easier to write. While recycling a single model has its valid use cases, it doesn't really help if we want to create multiple models (->count(10)) with our factory - all of them will be related to that same model.

This PR allows developers to provide their own, already created pool of models (collections), so they could be reused in all nested relations. For each relation, factory class picks a random model from a provided collection of models, instead of depending on a single one.
Sample usage:

    $users = User::all();
    $companies = Company::all();

    Posts::factory()
        ->recycle($users)
        ->recycle($companies)
        ->count(10)
        ->create();

For example, we often see that the User model relates to everything - as an author of some content, comments, reactions, performed operations etc. In more complex applications, that nesting can go deep. In that scenario, much easier would be to seed a number of users once and then reuse them in all nested relations (so we don't create a user only to be the author of a single comment). It's then closer to a real-life case, where we could have a set number of users in the system and all the actions performed and content created is within that group.

This approach leads to cleaner factories, less database pollution and overall higher seeding quality - created models will be much more related to each other.

New recycle method still allows for providing a single model so the PR is backward-compatible, without any breaking changes.

@taylorotwell
Copy link
Member

Revert all spacing changes so I can read the diff please.

@taylorotwell taylorotwell marked this pull request as draft September 28, 2022 13:27
@taylorotwell
Copy link
Member

Please mark as ready for review when that is done.

@michal3377
Copy link
Contributor Author

Sure! Sorry about that

@michal3377 michal3377 marked this pull request as ready for review September 28, 2022 13:47
@taylorotwell taylorotwell merged commit 50a1577 into laravel:9.x Oct 3, 2022
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