Skip to content

Commit 5b5c354

Browse files
authored
Create 2023-08-28-extending-laravel-workflow-to-support-spatie-laravel-tags.md
1 parent 8c8e40a commit 5b5c354

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
slug: extending-laravel-workflow-to-support-spatie-laravel-tags
3+
title: "Extending Laravel Workflow to Support Spatie Laravel Tags"
4+
authors:
5+
name: Richard
6+
title: Core Team
7+
url: https://github.com/rmcdaniel
8+
image_url: https://github.com/rmcdaniel.png
9+
tags: [laravel, workflow, spatie, tags, automation]
10+
---
11+
12+
![captionless image](https://miro.medium.com/v2/resize:fit:1400/format:webp/1*4YFhkvL6nZ3ny4NjGe6sMQ.png)
13+
14+
## One of the strengths of the Laravel ecosystem is its flexibility, thanks to a myriad of community-driven packages that enhance the framework’s capabilities. The `laravel-workflow` and `spatie/laravel-tags` packages are two such examples, and in this post, we'll integrate them together to make workflows taggable.
15+
16+
## Installation Instructions
17+
18+
Before diving into the code, let’s ensure both libraries are properly installed:
19+
20+
1. Install [Laravel Workflow](https://github.com/laravel-workflow/laravel-workflow) and [Spatie Laravel Tags](https://github.com/spatie/laravel-tags).
21+
```sh
22+
composer require laravel-workflow/laravel-workflow spatie/laravel-tags
23+
```
24+
25+
2. Both packages include migrations that must be published.
26+
```sh
27+
php artisan vendor:publish --provider="Workflow\Providers\WorkflowServiceProvider" --tag="migrations"
28+
php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-migrations"
29+
```
30+
31+
3. Run the migrations.
32+
```sh
33+
php artisan migrate
34+
```
35+
36+
## Publishing Configuration
37+
38+
To extend Laravel Workflow, publish its configuration file:
39+
```sh
40+
php artisan vendor:publish --provider="Workflow\Providers\WorkflowServiceProvider" --tag="config"
41+
```
42+
43+
## Extending Workflows to Support Tags
44+
45+
We need to extend the `StoredWorkflow` model of `laravel-workflow` to support tagging.
46+
47+
```php
48+
namespace App\Models;
49+
50+
use Spatie\Tags\HasTags;
51+
use Workflow\Models\StoredWorkflow as BaseStoredWorkflow;
52+
use Workflow\WorkflowStub;
53+
54+
class StoredWorkflow extends BaseStoredWorkflow
55+
{
56+
use HasTags;
57+
58+
public static function tag(WorkflowStub $workflow, $tag): void
59+
{
60+
$storedWorkflow = static::find($workflow->id());
61+
if ($storedWorkflow) {
62+
$storedWorkflow->attachTag($tag);
63+
}
64+
}
65+
66+
public static function findByTag($tag): ?WorkflowStub
67+
{
68+
$storedWorkflow = static::withAnyTags([$tag])->first();
69+
if ($storedWorkflow) {
70+
return WorkflowStub::fromStoredWorkflow($storedWorkflow);
71+
}
72+
}
73+
}
74+
```
75+
76+
## Modify the Configuration
77+
78+
In `config/workflow.php`, update this line:
79+
```php
80+
'stored_workflow_model' => Workflow\Models\StoredWorkflow::class,
81+
```
82+
To:
83+
```php
84+
'stored_workflow_model' => App\Models\StoredWorkflow::class,
85+
```
86+
This ensures Laravel Workflow uses the extended model.
87+
88+
## Running Tagged Workflows
89+
90+
With the taggable `StoredWorkflow` ready, create a console command to create, tag, retrieve, and run a workflow.
91+
92+
```php
93+
namespace App\Console\Commands;
94+
95+
use App\Models\StoredWorkflow;
96+
use App\Workflows\Simple\SimpleWorkflow;
97+
use Illuminate\Console\Command;
98+
use Workflow\WorkflowStub;
99+
100+
class Workflow extends Command
101+
{
102+
protected $signature = 'workflow';
103+
104+
protected $description = 'Runs a workflow';
105+
106+
public function handle()
107+
{
108+
// Create a workflow and tag it
109+
$workflow = WorkflowStub::make(SimpleWorkflow::class);
110+
StoredWorkflow::tag($workflow, 'tag1');
111+
112+
// Find the workflow by tag and start it
113+
$workflow = StoredWorkflow::findByTag('tag1');
114+
$workflow->start();
115+
116+
while ($workflow->running());
117+
118+
$this->info($workflow->output());
119+
}
120+
}
121+
```
122+
123+
## Conclusion
124+
125+
By integrating `laravel-workflow` with `spatie/laravel-tags`, we've enabled tagging for workflows, making management more intuitive in larger applications. Thanks to Laravel’s extensible nature, endless possibilities await developers leveraging these powerful packages.

0 commit comments

Comments
 (0)