What is the pipeline pattern?
The pipeline pattern passes a subject (the passable) through a series of pipes (processing steps) in sequence. Each pipe receives the output of the previous step, applies its transformation, and passes the result along. Laravel implements this pattern inIlluminate\Pipeline\Pipeline. Middleware processing is the most visible example — every HTTP request travels through a pipeline before reaching your controller.
Where Laravel uses pipelines
Pipelines are central to how Laravel’s core works.- HTTP middleware —
Illuminate\Foundation\Http\Kernelsends requests through a middleware pipeline. - Console commands — Artisan commands can be wrapped in pre/post processing.
- Route middleware — Route groups apply middleware via pipelines.
Basic usage
send / through / thenReturn
The simplest pipeline chains three calls.send($passable)— the value to send through the pipeline.through($pipes)— an array of pipes (closures or class names).thenReturn()— run the pipeline and return the result.
then
Usethen() when you want to specify a final callback explicitly.
pipe
Add pipes dynamically after construction withpipe().
Class-based pipes
Using dedicated pipe classes instead of closures improves reusability. Each pipe class implements ahandle method.
via — changing the method name
By default the pipeline callshandle on each pipe. Use via() to call a different method name.
Passing parameters to pipes
Append parameters to a class name using: and ,. This is the same syntax used by route middleware (throttle:60,1).
finally — running code after the pipeline
Register a callback that always runs after the pipeline completes, regardless of success or failure.withinTransaction — wrapping the pipeline in a transaction
Since Laravel 11, you can run an entire pipeline inside a database transaction.Practical example: order processing pipeline
1
Create the pipe classes
2
Run the pipeline
How the pipeline works internally
Thecarry() method is the heart of the pipeline. It uses array_reduce to fold the pipe array from right to left, building a chain of closures.
array_reduce so that the first pipe in your array is the first to execute. The closure stack is built from the inside out.
Using macros with Pipeline
Pipeline uses the Macroable trait, so you can add custom methods to it.
Next steps
The Macroable trait
Learn how to add custom methods to existing Laravel classes using the Macroable trait.