In this article
Introduction
Recently, I've been working on implementing two-factor authentication in a Laravel app. As part of this, I was reading through the Laravel Fortify code to see how the package handled two-factor authentication and generating recovery codes. After all, it's always good to see how the experts do it since as security is an important part of a web app!
While reading through the code, I came across a cool method that I hadn't seen before - the Collection::times()
method. I saw it being used in Fortify's src/Actions/EnableTwoFactorAuthentication.php file to generate the recovery codes for a user.
So, for anyone who hasn't also come across it yet, I thought I'd put together this Quickfire article to give a quick overview of what the Collection::times()
method is.
Using the Collection::times()
Method
The Collection::times()
allows you to create a new Collection by running a callback a specified number of times.
For example, let's say you want to generate 10 random strings. You could use the Collection::times()
method to do this like so:
1use Illuminate\Support\Collection; 2use Illuminate\Support\Str; 3 4$randomStrings = Collection::times( 5 number: 10, 6 callback: fn (): string => Str::random(8), 7); 8 9// $randomStrings is now a Collection with 10 random strings:10// [11// "aBcDeFgH",12// "iJkLmNoP",13// "qRsTuVwX",14// and so on...15// ]
As we can see in the example above, the method takes two arguments:
-
number
- The number of times to run the callback. -
callback
- The callback to run each time to generate the new item in the Collection.
The callback
also accepts the current iteration number as an argument. This can be useful if you need to use the current iteration number to generate the item.
For example, let's say you're building an application that tracks the amount of time it takes in seconds to complete a task in seconds. You might want to display the data in a chart that has regular intervals of 15 seconds. You could use the Collection::times()
method to generate the intervals like so:
1use Illuminate\Support\Collection; 2 3$intervals = Collection::times( 4 number: 10, 5 callback: fn (int $index): int => $index * 15, 6); 7 8// $intervals is now a Collection with 10 intervals: 9// [10// 15,11// 30,12// 45,13// and so on...14// ]
Of course, there are other ways that you could generate this data. But I feel like the Collection::times()
method is a nice, Laravel-y way of doing it. I also like the fact that it allows the code to be chainable as the result is a Collection so we can use any of the other available Collection methods on it.
This is definitely a method that I'll be making more use of in my own code in the future.
Conclusion
Hopefully, this article has given you a quick insight into the Collection::times()
method. Do you think this is something you might use yourself in your own projects?
You might also be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.
Or, you might want to check out my other 440+ ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter below.
Keep on building awesome stuff! 🚀