Why you should use Laravel Collections instead of PHP arrays
An Array is a basic data structure that stores one or many items in a single variable. PHP arrays have a massive problem with not being OOP. Illuminate\Support (Laravel) Collection comes with help.
Laravel Collection is a fluent OOP wrapper for working with arrays. It offers many great features. This approach allows us to create code that is clear, powerful, and easy to maintain.
1. Installation
Before we start using Collection, we must install a package. We have two possible approaches.
- The first approach - we can use the Laravel Framework where the Collections come from:
composer create-project --prefer-dist laravel/laravel collection-box
- The second approach is to install the Collection from a separate package, that has been provided by Laravel creators.
composer require illuminate/support
2. Basics
The package provides us with two ways of using Collection.
- We can create a Collection with a helper function.
- We can create a Collection by creating a new instance of Collection class.
It’s up to you what you choose. I prefer to use the helper function because I treat Collections as a new type of data in PHP.
3. Creating Collection
We can create a Collection from any type of variable. It will always be converted to an Iterable object with an OOP interface.
- An array
- A single number or a single string
- An array of objects
- Object* — The collection will be created from the public, protected and private properties.
* This is not the recommended way to use Collection.
4. Basic Usage
Let’s look at some basic functionalities, which can be used with Collections. Remember that Collections are implementing The Iterator Interface, so we can use basic PHP array functionalities with them.
- Getting from Collection
(https://laravel.com/docs/master/collections#method-get)
- Push to Collection without specifying a key
(https://laravel.com/docs/master/collections#method-push)
- Push to Collection with specifying key
(https://laravel.com/docs/master/collections#method-put)
- Iterate over Collection
(https://laravel.com/docs/master/collections#method-each)
- Unset element from Collection
(https://laravel.com/docs/master/collections#method-forget)
5. Some powerful methods
Let’s use some powerful features!
- Collection method: Map
(https://laravel.com/docs/master/collections#method-map)
- Collection method: Unique
(https://laravel.com/docs/master/collections#method-unique)
- Collection method: Pluck
(https://laravel.com/docs/master/collections#method-pluck)
- Collection method: SortBy
(https://laravel.com/docs/master/collections#method-sortby)
Full list of methods can be found here https://laravel.com/docs/master/collections#available-methods
6. Fluent Interface
The Collection uses the Fluent Interface Pattern. It provides a clear code.
7. Conclusions
Illuminate\Support Collection provides us with a great and powerful shell to work with arrays. Working with Collection radically has improved my workflow.
I have a small challenge for you. In your next project, change every array to Collection, and you will see a huge difference!
Happy coding! 🎈