lukeraymonddowning / poser

Create class based model factories in Laravel applications in seconds.
MIT License
276 stars 10 forks source link

Multiple arrays for attributes #47

Closed lukeraymonddowning closed 4 years ago

lukeraymonddowning commented 4 years ago

I have come across situations multiple times where I've wanted to create multiple models but provide different attributes to each one. This is also true for pivot records across multiple models.

It would be nice to have a syntax like:

UserFactory::times(2)
    ->withAttributes(['name' => 'Joe Bloggs'], ['name' => 'Jane Bloggs'])
    ->create();

This would apply to any method where attributes can be passed in:

UserFactory::times(2)
    ->create(['name' => 'Joe Bloggs'], ['name' => 'Jane Bloggs']);

In a situation where there were more models to be created than attributes provided...

UserFactory::times(5)
    ->create(['name' => 'Joe Bloggs'], ['name' => 'Jane Bloggs']);

...it would apply the attributes sequentially, looping back to the first until exhausted. So in this example, you'd get: 'Joe Bloggs', 'Jane Bloggs', 'Joe Bloggs', 'Jane Bloggs', 'Joe Bloggs'.

In a situation where there were more attributes than models to be created, it would simply ignore those additional attributes.