Supercharged pipelines for Laravel
Published on by Paul Redmond
The chefhasteeth/pipeline package for Laravel adds a few unique features to the built-in pipeline functionality. For example, this package has a withTransaction()
method, which will run this pipeline within a database transaction and automatically commit (or roll back) depending on if the pipeline succeeded:
use Chefhasteeth\Pipeline\Pipeline; // i.e., controller methodpublic function store(StoreRegistrationRequest $request){ return Pipeline::make() ->withTransaction() ->send($request->all()) ->through([ RegisterUser::class, AddMemberToTeam::class, SendWelcomeEmail::class, ]) ->then(fn ($data) => UserResource::make($data));}
Next, this package also has a Pipable
trait that you could implement on a data object or class:
use Chefhasteeth\Pipeline\Pipable; class UserDataObject{ use Pipable; public string $name; public string $email; public string $password; // ...} // Run the pipelinereturn UserDataObject::fromRequest($request) ->pipeThrough([ RegisterUser::class, AddMemberToTeam::class, SendWelcomeEmail::class, ]) ->then(fn ($data) => UserResource::make($data));
You can learn about this package, get full installation instructions, and view the source code on GitHub. If you'd like to learn more about pipelines in Laravel, Jeff Ochoa wrote Understanding Laravel Pipelines, which is an excellent resource.