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

recycle() method #794

Open
nikophil opened this issue Jan 19, 2025 · 0 comments
Open

recycle() method #794

nikophil opened this issue Jan 19, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@nikophil
Copy link
Member

I do think a recycle() method, similar to what Laravel factories provide could be useful.

While reading some blog posts about it, I figured out that one of its usage is basically when we do something like Contact::new(['address' => AddressFactory::createOne())->many(5)->create(), and I'm not interested about this part of the feature.

What I like about it, is that it forces to reuse deeply the same object, for every ManyToOne relationship.

Given their example with Airline, Flight, and Ticket models, where the ticket belongs to an airline and a flight, and the flight also belongs to an airline, the following code will create only one Airline, and use it both for Ticket::$airline relationship and for Flight::$airline relationship:

 Ticket::new()
    ->recycle(Airline::createOne())
    ->create();

As far as I understand how it works, recycle() only applies for *ToOne relationships.

It also accepts a collection of objects:

$airlines = Airline::createMany(2);

 Ticket::new()
    ->recycle($airlines)
    ->many(5)
    ->create();

But I don't know if in this case, we should ensure the integrity of the data and always pick the same airline for both the ticket and its flight. I'm tempted to say "yes", because for me the whole point of this recycle() method would be to ensure integrity of the model. As far as I understand, this would differ from Laravel's recycle() method, where they always pick data randomly from the recycle collection.

any thought @kbond ?

@nikophil nikophil added the enhancement New feature or request label Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

1 participant