Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

How to use Collections outside of Laravel

The one feature of Laravel I truly admire is the Collections. Essentially, collections provide a fluent, convenient wrapper for working with arrays of data.

This makes working with arrays a breeze since you have got a fluent API at your disposal and under the hood, Collections implements the PHP array methods only but the developer experience gets a lot better.

A little intro to Collections

So, let’s say if you have the following array…

$colors = ['red', 'green', 'blue'];

…And if you want to map it over the array items and prepend “color” to each of them, here’s how we can do it using the Laravel collections.

use Illuminate\Support\Collection;

$colors = ['red', 'green', 'blue'];
$collection = collect($colors);

$updatedColors = $collection->map(function ($item, $key) {
    return 'color '.$item;
});

$updatedColors->all();
// ["color red","color green","color blue"]

Now, collections come packed with Laravel out-of-the-box. So, you can just start using them.

But let’s say, you’re not using Laravel and you still want to use Collections in your non-Laravel PHP projects, how would you do it?

Well, we are going to see how we can do it in the next section.

Using Collections Elsewhere

To use Laravel collections in your non-Laravel projects, it’s a pre-requisite that you’re using Composer in your project.

If you do, go ahead and install the illuminate/collections package in your project like so.

$ composer require illuminate/collections

This will install latest stable version of the illuminate/collections package and all its dependencies in your project.

And that’s it! You can now start using the Collections just like how you would use it in your Laravel project.

So, let’s say, you have a file src/index.php in your project and you want to use Collections in it, you can do it like so.

include './vendor/autoload.php';

use Illuminate\Support\Collection;

Collection::macro('toUpper', function () {
    return $this->map(function ($value) {
        return strtoupper($value);
    });
});

$collection = collect(['first', 'second']);

$upper = $collection->toUpper();

printf($upper);
// ["FIRST","SECOND"]

And that’s about it!

You can start using 100s of Collection methods in your non-Laravel project just like that!

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?