Supercharged pipelines for Laravel

Packages

June 15th, 2022

Supercharged pipelines for Laravel

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:

1use Chefhasteeth\Pipeline\Pipeline;
2 
3 
4// i.e., controller method
5public function store(StoreRegistrationRequest $request)
6{
7 return Pipeline::make()
8 ->withTransaction()
9 ->send($request->all())
10 ->through([
11 RegisterUser::class,
12 AddMemberToTeam::class,
13 SendWelcomeEmail::class,
14 ])
15 ->then(fn ($data) => UserResource::make($data));
16}

Next, this package also has a Pipable trait that you could implement on a data object or class:

1use Chefhasteeth\Pipeline\Pipable;
2 
3class UserDataObject
4{
5 use Pipable;
6 
7 public string $name;
8 public string $email;
9 public string $password;
10 // ...
11}
12 
13// Run the pipeline
14return UserDataObject::fromRequest($request)
15 ->pipeThrough([
16 RegisterUser::class,
17 AddMemberToTeam::class,
18 SendWelcomeEmail::class,
19 ])
20 ->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.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.