Tutorial by Examples

Using the collect() helper, you can easily create new collection instances by passing in an array such as: $fruits = collect(['oranges', 'peaches', 'pears']); If you don't want to use helper functions, you can create a new Collection using the class directly: $fruits = new Illuminate\Support\Co...
You can select certain items out of a collection by using the where() method. $data = [ ['name' => 'Taylor', 'coffee_drinker' => true], ['name' => 'Matt', 'coffee_drinker' => true] ]; $matt = collect($data)->where('name', 'Matt'); This bit of code will select all it...
You often find yourself in a situation where you need to find a variables corresponding value, and collections got you covered. In the example below we got three different locales in an array with a corresponding calling code assigned. We want to be able to provide a locale and in return get the as...
A common problem is having a collection of items that all need to meet a certain criteria. In the example below we have collected two items for a diet plan and we want to check that the diet doesn't contain any unhealthy food. // First we create a collection $diet = collect([ ['name' => ...
You will often find yourself with a collection of data where you are only interested in parts of the data. In the example below we got a list of participants at an event and we want to provide a the tour guide with a simple list of names. // First we collect the participants $participants = colle...
Often you need to change the way a set of data is structured and manipulate certain values. In the example below we got a collection of books with an attached discount amount. But we much rather have a list of books with a price that's already discounted. $books = [ ['title' => 'The Pragma...
Collections also provide you with an easy way to do simple statistical calculations. $books = [ ['title' => 'The Pragmatic Programmer', 'price' => 20], ['title' => 'Continuous Delivery', 'price' => 30], ['title' => 'The Clean Coder', 'price' => 10], ] $min = col...
There are a several different ways of sorting a collection. Sort() The sort method sorts the collection: $collection = collect([5, 3, 1, 2, 4]); $sorted = $collection->sort(); echo $sorted->values()->all(); returns : [1, 2, 3, 4, 5] The sort method also allows for passing in ...
The reduce method reduces the collection to a single value, passing the result of each iteration into the subsequent iteration. Please see reduce method. The reduce method loops through each item with a collection and produces new result to the next iteration. Each result from the last iteration ...
The macro() function allows you to add new functionality to Illuminate\Support\Collection objects Usage: Collection::macro("macro_name", function ($parameters) { // Your macro }); For example: Collection::macro('uppercase', function () { return $this->map(function ($i...
The Collection object implements the ArrayAccess and IteratorAggregate interface, allowing it to be used like an array. Access collection element: $collection = collect([1, 2, 3]); $result = $collection[1]; Result: 2 Assign new element: $collection = collect([1, 2, 3]); $collection[] = ...

Page 1 of 1