Scheduled Triggers (CRON)
Create database-driven schedules that automatically dispatch workflows on a recurring basis using CRON expressions.
Creating a Schedule
php
use Workflowable\Workflowable\Models\WorkflowSchedule;
use App\WorkflowEvents\DailyReport;
WorkflowSchedule::create([
'name' => 'daily_report',
'event_name' => 'daily_report',
'workflow_event' => new DailyReport(reportType: 'daily'),
'cron' => '0 9 * * *', // Every day at 9am
'is_enabled' => true,
]);The workflow_event field stores a serialized WorkflowEvent DTO, including any encrypted fields.
Registering the Schedule Command
Add to your routes/console.php or Laravel scheduler:
php
Schedule::command('workflowable:schedule')->everyMinute();The command evaluates each enabled schedule's CRON expression against last_run_at to determine if it's due, then dispatches the workflow via Workflowable::dispatch() (queued).
Schedule Fields
| Field | Type | Description |
|---|---|---|
name | string | Human-readable schedule name |
event_name | string | Registered event name from config |
workflow_event | WorkflowEvent | The event DTO to dispatch |
cron | string | CRON expression |
is_enabled | boolean | Enable/disable the schedule |
last_run_at | timestamp | Last successful dispatch time |
Managing Schedules
php
// Disable a schedule
$schedule->update(['is_enabled' => false]);
// Change the CRON expression
$schedule->update(['cron' => '0 */6 * * *']); // Every 6 hours
// Update the event data
$schedule->update([
'workflow_event' => new DailyReport(reportType: 'weekly'),
]);Duplicate Prevention
The schedule command uses last_run_at to prevent duplicate dispatches. If the command runs multiple times within the same CRON interval, the workflow is only dispatched once.