Laravel Pipeline Pattern Beyond Middleware | Mohamed Said        [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com) [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles  ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

       [  ](https://github.com/EG-Mohamed)       

 [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

  [ home ](https://msaied.com)    [ articles ](https://msaied.com/articles)    The Pipeline Pattern in Laravel: Building Custom Pipelines Beyond Middleware        On this page       1. [  The Pipeline Pattern in Laravel: Beyond HTTP Middleware ](#the-pipeline-pattern-in-laravel-beyond-http-middleware)
2. [  Why Pipelines Over Plain Method Chains? ](#why-pipelines-over-plain-method-chains)
3. [  The Core API ](#the-core-api)
4. [  Writing a Stage ](#writing-a-stage)
5. [  Dynamic Stage Selection ](#dynamic-stage-selection)
6. [  A Typed Pipeline Wrapper ](#a-typed-pipeline-wrapper)
7. [  Testing Stages in Isolation ](#testing-stages-in-isolation)
8. [  When Not to Use a Pipeline ](#when-not-to-use-a-pipeline)
9. [  Takeaways ](#takeaways)

  ![The Pipeline Pattern in Laravel: Building Custom Pipelines Beyond Middleware](https://cdn.msaied.com/359/fd848015c1e33ba7df4a1e9598454b5e.png)

  #laravel   #design-patterns   #pipeline   #clean-architecture  

 The Pipeline Pattern in Laravel: Building Custom Pipelines Beyond Middleware 
==============================================================================

     4 Jul 2026      3 min read    ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said  

       Table of contents

  9 sections  

1. [  01   The Pipeline Pattern in Laravel: Beyond HTTP Middleware  ](#the-pipeline-pattern-in-laravel-beyond-http-middleware)
2. [  02   Why Pipelines Over Plain Method Chains?  ](#why-pipelines-over-plain-method-chains)
3. [  03   The Core API  ](#the-core-api)
4. [  04   Writing a Stage  ](#writing-a-stage)
5. [  05   Dynamic Stage Selection  ](#dynamic-stage-selection)
6. [  06   A Typed Pipeline Wrapper  ](#a-typed-pipeline-wrapper)
7. [  07   Testing Stages in Isolation  ](#testing-stages-in-isolation)
8. [  08   When Not to Use a Pipeline  ](#when-not-to-use-a-pipeline)
9. [  09   Takeaways  ](#takeaways)

       The Pipeline Pattern in Laravel: Beyond HTTP Middleware
-------------------------------------------------------

Most Laravel developers know `Pipeline` only as the engine behind HTTP middleware. But `Illuminate\Pipeline\Pipeline` is a first-class, general-purpose primitive you can reach for whenever you need to pass a payload through an ordered sequence of transformations — order processing, CSV imports, AI prompt enrichment, multi-step validation, and more.

### Why Pipelines Over Plain Method Chains?

A plain method chain couples every step to the caller. Pipelines decouple the *what* (the payload) from the *how* (the stages), letting you:

- Swap, reorder, or skip stages at runtime
- Test each stage in complete isolation
- Resolve stages from the service container (constructor injection works automatically)

### The Core API

```php
use Illuminate\Pipeline\Pipeline;

$result = app(Pipeline::class)
    ->send($payload)
    ->through([
        NormaliseInput::class,
        ValidateBusinessRules::class,
        PersistOrder::class,
        DispatchConfirmationEmail::class,
    ])
    ->thenReturn();

```

`thenReturn()` returns the (possibly mutated) payload after all stages. Use `then(fn ($p) => ...)` when you need a final transformation step.

### Writing a Stage

A stage is any class with a `handle` method — or any callable. The signature mirrors middleware:

```php
final class NormaliseInput
{
    public function handle(OrderData $data, Closure $next): OrderData
    {
        $data = $data->withNormalisedEmail(
            mb_strtolower(trim($data->email))
        );

        return $next($data);
    }
}

```

Because stages are resolved via the container, you can inject repositories, config, or services freely:

```php
final class ValidateBusinessRules
{
    public function __construct(
        private readonly ProductRepository $products,
    ) {}

    public function handle(OrderData $data, Closure $next): OrderData
    {
        foreach ($data->lines as $line) {
            throw_unless(
                $this->products->isAvailable($line->sku, $line->qty),
                InsufficientStockException::class,
                "SKU {$line->sku} is out of stock."
            );
        }

        return $next($data);
    }
}

```

### Dynamic Stage Selection

Stages don't have to be a static array. Build them at runtime based on context:

```php
$stages = collect([
    NormaliseInput::class,
    ValidateBusinessRules::class,
])
->when($order->requiresExportCompliance(), fn ($c) =>
    $c->push(ExportComplianceCheck::class)
)
->push(PersistOrder::class)
->all();

app(Pipeline::class)->send($order)->through($stages)->thenReturn();

```

### A Typed Pipeline Wrapper

For domain code, a thin wrapper improves discoverability and enforces the payload type:

```php
final class OrderProcessingPipeline
{
    public function __construct(private readonly Pipeline $pipeline) {}

    /** @param class-string[] $extraStages */
    public function process(OrderData $data, array $extraStages = []): OrderData
    {
        return $this->pipeline
            ->send($data)
            ->through([
                NormaliseInput::class,
                ValidateBusinessRules::class,
                ...$extraStages,
                PersistOrder::class,
            ])
            ->thenReturn();
    }
}

```

Bind it as a singleton and inject it wherever needed. Callers never touch the raw `Pipeline` class.

### Testing Stages in Isolation

Because each stage is a plain class, unit testing is trivial:

```php
it('normalises the email to lowercase', function () {
    $stage = new NormaliseInput();
    $data  = OrderData::fake(['email' => '  USER@Example.COM  ']);

    $result = $stage->handle($data, fn ($d) => $d);

    expect($result->email)->toBe('user@example.com');
});

```

No HTTP context, no database, no framework bootstrap required.

### When Not to Use a Pipeline

Pipelines shine when stages are interchangeable and the payload flows linearly. Avoid them when:

- Stages need to communicate laterally (use a saga or process manager instead)
- The flow is highly conditional with many branches (a state machine is clearer)
- You only have one or two steps (a simple service method is less indirection)

### Takeaways

- `Illuminate\Pipeline\Pipeline` is a general-purpose tool, not just for HTTP middleware.
- Stages are container-resolved classes — constructor injection works out of the box.
- Wrap the raw pipeline in a typed domain class to enforce payload contracts.
- Each stage is independently unit-testable with a simple closure as the `$next` stub.
- Build stage lists dynamically with `collect()->when()` for context-sensitive flows.

 Found this useful?

          [  ](https://twitter.com/intent/tweet?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fthe-pipeline-pattern-in-laravel-building-custom-pipelines-beyond-middleware-1&text=The+Pipeline+Pattern+in+Laravel%3A+Building+Custom+Pipelines+Beyond+Middleware) [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fthe-pipeline-pattern-in-laravel-building-custom-pipelines-beyond-middleware-1) 

 Frequently Asked Questions 
----------------------------

  3 questions  

     Q01  Can I use closures as pipeline stages instead of classes?        Yes. The `through()` method accepts any iterable of callables, including closures. Classes are preferred in production code because they are container-resolved, named, and independently testable, but closures are handy for quick ad-hoc stages or in tests. 

      Q02  How do I stop the pipeline early, for example on a validation failure?        Simply throw an exception inside the stage and do not call `$next`. Wrap the `thenReturn()` call in a try/catch at the call site. This keeps each stage's responsibility clear and avoids boolean flags polluting the payload. 

      Q03  Is there a performance cost to using the Pipeline class compared to direct method calls?        The overhead is negligible for typical domain workflows — a handful of container resolutions and closure calls. Only consider alternatives if you are running thousands of pipeline executions per request in a tight loop, which would be unusual. 

  Continue reading

 More Articles 
---------------

 [ View all    ](https://msaied.com/articles) 

 [ ![Read/Write Splitting, Connection Pooling, and Sticky Reads in Laravel](https://cdn.msaied.com/358/da422e65efc54b8046de1eaf23f03e90.png) laravel database postgresql 

### Read/Write Splitting, Connection Pooling, and Sticky Reads in Laravel

Learn how Laravel's database manager handles read/write splitting, when sticky reads matter, and how to layer...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 4 Jul 2026     4 min read  

  Read    

 ](https://msaied.com/articles/readwrite-splitting-connection-pooling-and-sticky-reads-in-laravel-3) [ ![Laravel Service Container: Contextual Binding, Tagging, and Method Injection](https://cdn.msaied.com/357/6ee56a71454e019ced2bd70790e9ca60.png) laravel service-container dependency-injection 

### Laravel Service Container: Contextual Binding, Tagging, and Method Injection

Go beyond basic singleton registration. Learn how contextual binding, container tags, and method injection let...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 4 Jul 2026     3 min read  

  Read    

 ](https://msaied.com/articles/laravel-service-container-contextual-binding-tagging-and-method-injection-2) [ ![PostgreSQL JSONB in Laravel: Indexing, Querying, and Schema-less Columns Done Right](https://cdn.msaied.com/356/592e07bf3b46443cb950d9bb31725ba6.png) laravel postgresql eloquent 

### PostgreSQL JSONB in Laravel: Indexing, Querying, and Schema-less Columns Done Right

JSONB columns can replace entire pivot tables or EAV nightmares — if you index them correctly and query throug...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 3 Jul 2026     3 min read  

  Read    

 ](https://msaied.com/articles/postgresql-jsonb-in-laravel-indexing-querying-and-schema-less-columns-done-right) 

   [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

Explore

- [Home](https://msaied.com)
- [Projects](https://msaied.com/projects)
- [Articles](https://msaied.com/articles)
- [Certificates](https://msaied.com/certificates)
- [Contact](https://msaied.com#contact-section)

Connect

- [   hello@msaied.com ](mailto:hello@msaied.com)
- [   +20 109 461 9204 ](tel:+201094619204)

© 2026 Mohamed Said. All rights reserved.

 [  ](https://github.com/EG-Mohamed) [  ](https://www.linkedin.com/in/msaiedm/) [  ](https://wa.me/201094619204) [  ](mailto:hello@msaied.com) [  ](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
