logo
podcast Podcast
get help Get Unstuck

Easily generate a bigger collection

Useful for quick experimentation

Joel Clermont
Joel Clermont
2024-10-03

When you're building out a new API endpoint, you'll often want to test it with a variety of data sets.

It can get a little tedious to create a bunch of records manually or create elaborate seeders for every scenario.

For example, if you want to quickly confirm that pagination is working as expected.

One technique that can be handy is to use the new multiply method on collections that was added in Laravel 11.12.

With this approach, if you have a small static dataset, you can easily chain on the multiply method to increase it further:

$sampleData = collect([
    ['name' => 'Joel'],
    ['name' => 'Aaron'],
]);

// expand from 2 items to 10 items
return $sampleData->multiply(5);

This could also be useful for quickly generating a larger collection of data inside a test too.

Here to help,

Joel

P.S. Download our free security ebook and check 7 quick ways to make your app more secure.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.