Laravel Collections – times() Method
Laravel Collections ( 11 Lessons )
Laravel Collections is one of the most powerful features of Laravel. Basically, collections are PHP arrays but it’s an Object Oriented approach to deal with PHP arrays.
see full series- Laravel Collections – Introduction to Collections
- Laravel Collections – make Method
- Laravel Collections – wrap() and unwrap() Methods
- Laravel Collections – map() Method
- Laravel Collections – times() Method
- Laravel Collections – collapse() Method
- Laravel Collections – contains() & containsStrict() Methods
- Laravel Collections – each() Method
- Laravel Collection – Using toJson() Method
- Laravel Collection – Filtering Collection Items using where()
- Laravel Collection – Using splice() Method to Remove Items
In this post, we will look at how we can use the times()
method to quickly create numbers of Laravel Collections.
The times()
method of Laravel Collection is similar to PHP’s range() function with some extra functionality for Collections.
Signature
Below is the signature of the times()
method in Illuminate\Support\Collection class.
/** * Create a new collection by invoking the callback a given amount of times. * * @param int $number * @param callable $callback * @return static */ public static function times($number, callable $callback = null) { if ($number < 1) { return new static; } if (is_null($callback)) { return new static(range(1, $number)); } return (new static(range(1, $number)))->map($callback); }
As you can see this method takes two arguments $number
and a callback function. The $number
argument controls the number of elements that will be created within the collection.
Example
The following example demonstrates how to populates a Laravel Collections with a range of numbers.
use Illuminate\Support\Collection; // Creating a collection containing all numbers from 1 to 4 $collection = Collection::times(4);
The above code will create a collection containing the numbers from one to four. The collection’s times()
method always start it’s range from 1. If we provide a negative number for $number
, Laravel will create an empty collection.
This method becomes more useful when we use the second argument by passing a callback function. When a callback function is provided, Laravel Collections will invoke the callback function for the specified number of times. The callback function receives the index of the current iteration as it’s the first argument.
We can use the times()
method to create a list of next 7 days using the Carbon instance which is available in Laravel out of the box.
use Illuminate\Support\Collection; $nextWeek = Collection::times(7, function ($index) { return \Carbon\Carbon::now()->addDay($index); }); dd($nextWeek); Collection {#522 ▼ #items: array:7 [▼ 0 => Carbon @1556722932 {#510 ▼ date: 2019-05-01 15:02:12.999094 UTC (+00:00) } 1 => Carbon @1556809332 {#516 ▼ date: 2019-05-02 15:02:12.999129 UTC (+00:00) } 2 => Carbon @1556895732 {#517 ▼ date: 2019-05-03 15:02:12.999157 UTC (+00:00) } 3 => Carbon @1556982132 {#518 ▼ date: 2019-05-04 15:02:12.999185 UTC (+00:00) } 4 => Carbon @1557068532 {#519 ▼ date: 2019-05-05 15:02:12.999212 UTC (+00:00) } 5 => Carbon @1557154932 {#520 ▼ date: 2019-05-06 15:02:12.999239 UTC (+00:00) } 6 => Carbon @1557241332 {#521 ▼ date: 2019-05-07 15:02:12.999265 UTC (+00:00) } ] }
Stay tuned for the upcoming Laravel Collection posts.