• 9 min read
14 Must Know Laravel Collection Methods
When working with Eloquent, your data will always result in a custom collection object, which is a class that is inherited from the Laravel Collections. It provides a lot of awesome methods that you can chain into your collection. In this article, we will cover 14 collection methods that you must know when working with Eloquent.
Before we dive into the collection methods, I want to make sure that we both have data inside our database. The easiest way of adding dummy data inside the database is by interacting with the users table. Open the /database/seeders/DatabaseSeeder.php
file and replace the run()
method with:
public function run()
{
\App\Models\User::factory(50)->create();
}
This command will create 50 new users’ inside your users table.
Before we can use our users’ table, we need to make sure that we migrate our migrations and seeder.
php artisan migrate --seed
Custom Artisan Command & Routes
Instead of constantly navigating to the browser, we are going to use a custom artisan command that will run the index()
method from our controller, inside the CLI.
Let’s perform the following two lines of code to create our custom Artisan command and our Controller.
php artisan make:command ShowMethod --command=show:method
php artisan make:controller CollectionsController
Add the following method inside the CollectionsController.php
file:
public function index()
{
$collection = collect([1, 2, 3, 4, 5]);
}
We need to make sure that we call this method inside the handle()
method of our /app/Console/Commands/ShowMethod.php
file.
public function handle()
{
$collectionsController = new CollectionsController();
print_r($collectionsController->index());
}
This will create a new instance of the CollectionsController named ($collectionsController)
, which will then print out the available index()
method inside of it.
From now on, whenever we make changes inside the index()
method, we can simply run the following command to get output inside the CLI.
php artisan show:method
1. avg()
The avg() method returns the average value of a given key
.
public function index()
{
return collect([1, 2, 3, 4, 5])->avg();
}
Output: 3
Wondering what the difference will be with PHP? To achieve the same functionality, your PHP code will look like this:
public function index()
{
$collection = collect([1, 2, 3, 4, 5]);
$average = array_sum($collection)/count($collection);
echo $average;
}
Output: 3
2. chunk()
The chunk method will break or split a large group of data into smaller groups. The chunk()
method accepts one parameter, which will be the amount you want to split your collection with, in the example down below, it will be 2.
public function index()
{
return collect([1, 2, 3, 4, 5])->chunk(2);
}
Output
(
[items:protected] => Array
(
[0] => 1
[1] => 2
)
),
(
[items:protected] => Array
(
[2] => 3
[3] => 4
)
),
(
[items:protected] => Array
(
[4] => 5
)
)
3. combine()
The combine method is pretty useful since it will combine the values of the collection as keys, with the value of another array or collection.
public function index()
{
$collection = collect(['name', 'language']);
return $collection->combine((['Laravel', 'PHP']));
}
Output
(
[name] => Laravel
[language] => PHP
)
4. contains()
The contains() method will find out whether a collection contains a given item that you pass in as a closure.
public function index()
{
$collection = collect([1, 2, 3, 4, 5]);
ray($collection->contains(5));
}
Output
True
Once you output it through ray, true
will be printed out and if you return the $collection->contains(5));
you will receive a 1
.
5. count()
The count() method will count the values of an array
public function index()
{
$collection = collect([1, 2, 3, 4, 5]);
return $collection->count();
}
Output
5
6. diff()
The diff() method compares two collections/arrays. It will then return the values in the original collection that are not present in the given collection.
public function index()
{
$collection = collect([1, 2, 3, 4, 5]);
return $collection->diff([2, 5, 6, 8]);
}
Output
(
[0] => 1
[2] => 3
[3] => 4
)
The original collection is $collection
so therefore value 1, 3 and 4 are printed out.
7. dd()
We have all used the dd() method inside the browser once in our lives. The dd() method on a collection does the same thing.
public function index()
{
$collection = collect([1, 2, 3, 4, 5]);
return $collection->dd();
}
array:5 [
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
]
The output will be in the same way as you usually see inside the browser.
8. flip()
I find() flip a pretty interesting and funny method because it will flip all the item’s keys in a collection with their given values.
public function index()
{
$collection = collect([
'name' => 'Dary',
'job' => 'Web Developer',
'country' => 'the Netherlands'
]);
return $collection->flip();
}
Output
(
[Dary] => name
[Web Developer] => job
[the Netherlands] => country
)
The output tells us that [Dary], [Web Developer] and [the Netherlands]
are not our values anymore, but they have been flipped to our keys, and name, job and country
are not our keys anymore but they have been flipped to our values.
9. forget()
The forget() method removes an item from the collection by the given key.
public function index()
{
$collection = collect([
'name' => 'Dary',
'job' => 'Web Developer',
'country' => 'the Netherlands'
]);
return $collection->forget('name');
}
Output
(
[job] => Web Developer
[country] => the Netherlands
)
10. each()
Here comes the section with a couple more complex methods. The each() method is nothing more than a for each loop that is wrapped inside a higher-order function. A higher-order function is a function that takes another function as a parameter, returns a function, or does both.
public function index()
{
$users = User::all();
$users->each(function ($value, $key) {
$user = $value['name'];
echo $user . ' | ';
});
}
Output
Prof. Marc Mueller DVM | Caesar Hammes | Devan Mertz | Mrs. Claudie O'Reilly | Mrs. Kenya McLaughlin Sr. | Melba Sauer III | Arlo Ullrich |
What the each()
method will do is iterate over all users that we got inside our $users
collection, and print out the names of all users.
Let’s see the difference between the each method vs what it will look like in ‘normal’ PHP:
public function index()
{
$users = User::all();
$allUsers = [];
foreach($users as $user => $key) {
$allUsers = $user['name'];
}
echo $allUsers;
}
The biggest difference is the fact that we are hiding the implementation of the for each loop. Why would you define an empty array above your loop, set it equal to a value inside the loop, and then print it returning it outside of the loop if you could use the each()
method?
11. map()
The map() method iterates through the $users
collection and passes each value to the given callback.
public function index()
{
$users = User::all();
$users->map(function ($item, $key){
print_r($item['name']);
});
}
Output
Prof. Marc Mueller DVM Caesar Hammes Devan MertzMrs. Claudie O'Reilly Mrs. Kenya McLaughlin Sr. Melba Sauer III Arlo Ullrich
When should you be using the map method?
• When you want to extract a field from an array of objects
• Populating an array of objects from raw data
• Converting items into a new format
12. filter()
The filter() method is used to filter out elements of an array that you don’t want to use. You got to tell the filter which elements you want to keep by passing a callback that returns true if you want to keep the element, and false if you want to remove it.
public function index()
{
$users = User::all();
$filtered = $filtered = $users->filter(function ($value, $key) {
return $value['id'] > 48;
});
return $filtered->all();
}
Output
The last two users inside your ```$users``` collection
13. pluck()
A list of Collections methods can’t miss the pluck() method. The pluck() method retrieves all values for a given key.
public function index()
{
$users = User::all();
return $users->pluck('name');
}
Output
(
[0] => Prof. Marc Mueller DVM
[1] => Caesar Hammes
[2] => Devan Mertz
[3] => Mrs. Claudie O'Reilly
[4] => Mrs. Kenya McLaughlin Sr.
[5] => Melba Sauer III
[6] => Arlo Ullrich
...........
}
14. isEmpty() & isNotEmpty()
You can use the isEmpty() and isNotEmpty() methods to check if your collection is empty or not.
public function index()
{
$users = User::all();
dd($users->isEmpty());
}
Output
False
public function index()
{
$users = User::all();
dd($users->isNotEmpty());
}
Output
True
Our $users
collection is not empty since the value consists of a collection of all of our users. Therefore, the output of the isEmpty()
method is false and the output of isNotEmpty()
is true.
Extra: Chaining multiple methods
The power of collections come in clutch when you want to chain multiple methods together. Let’s put a couple of the methods we’ve discussed together and see what we could achieve.
public function index()
{
$users = User::all();
return $users
->pluck('name')
->except('id', 49)
->forget('Bethany Parker')
->skip(45);
}
Output
(
[45] => Kaleigh Gorczany
[46] => Clifton Lowe
[47] => Nick Auer
[48] => Prof. Korey Strosin DDS
)
Alright, let’s go over the methods we chained together:
- pluck() makes sure that we’re only grabbing the
$users
name - except() prints out all
$users
except the user with id 49 - forget() removes the user
Bethany Parker
from the list - skip() skips the first 45 rows
Conclusion
In my opinion, collections are the most powerful feature that Laravel has to offer. It makes your entire coding process a lot easier. There are a ton of other methods available that you could chain on your collections, which you can find right here.