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

Amit Merchant

A blog on PHP, JavaScript, and more

The pipeline helper in Laravel

The Pipeline facade is one little helper in Laravel that has been around for quite some time now but it’s only recently that it has got its place in the Laravel documentation.

As Taylor mentioned, the class has been used internally in Laravel for a long time but it was never documented.

The Pipeline helper

Essentially, the Pipeline class is a simple implementation of the pipeline pattern in PHP. It’s a great way to chain multiple callbacks together and pass the result of one callback to the next callback in a sequential manner.

Let’s see how we can use the Pipeline class in Laravel with a simple example.

use Illuminate\Support\Facades\Pipeline;
use Illuminate\Support\Str;

$postSlug = Pipeline::send('Laravel pipelines are awesome!')
            ->through([
                function (string $title, \Closure $next) {
                    $title = Str::slug($title);
 
                    return $next($title);
                },
                function (string $title, \Closure $next) {
                    $title = Str::upper($title);
 
                    return $next($title);
                },
            ])
            ->then(fn (string $title) => $title);

dd($postSlug);

// "LARAVEL-PIPELINES-ARE-AWESOME"

This one is a pretty broad example but it will help you understand the concept of the Pipeline class.

As you can tell, in the example above, we’re passing the string “Laravel pipelines are awesome!” to the Pipeline class, and then we’re chaining two callbacks using the through() method. The first callback will convert the string to a slug and the second callback will convert the slug to uppercase.

The $next parameter in the callback is a closure that will pass the result of the current callback to the next callback.

Finally, we’re returning the result (the processed string) of the last callback using the then() method when that callback is executed.

Using invokable classes

On top of this, you can also pass invokable classes to the Pipeline class as well.

$postSlug = Pipeline::send('Laravel pipelines are awesome!')
            ->through([
                Slugify::class,
                Uppercase::class,
            ])
            ->then(fn (string $title) => $title);

The Slugify class, for instance, could look like this.

class Slugify
{
    public function __invoke(string $title, \Closure $next)
    {
        $title = Str::slug($title);

        return $next($title);
    }
}

In closing

Even though the example above is pretty simple, the Pipeline class can be used to do some pretty complex things and there are a lot of use cases for it. But for now, I hope you got a good idea of how you can use the Pipeline class in Laravel.

Until next time!


Prefer video format instead? Fret not!

Learn the fundamentals of PHP 8 (including 8.1, 8.2, 8.3, and 8.4), 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?

Buy me a coffee

👋 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?