Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Model-Based Scheduling for Laravel with Cadence

Last updated on by

Model-Based Scheduling for Laravel with Cadence image

Cadence by Steve Bauman takes a different approach to scheduling in Laravel. Rather than centralising all timed tasks in a single scheduler file, it lets you attach one or many schedules directly to individual Eloquent model instances, each with its own expression and timezone, and fires events when they're due.

How It Works

After installing the package and publishing the migration, you get a schedules table that holds a polymorphic reference to any model, the schedule expression, an optional timezone, and precomputed next_run_at / last_run_at timestamps.

Add the Schedulable interface and HasSchedules trait to any model you want to schedule:

use DirectoryTree\Cadence\HasSchedules;
use DirectoryTree\Cadence\Schedulable;
 
class Subscription extends Model implements Schedulable
{
use HasSchedules;
}

Then attach a schedule to a model instance. Cadence ships with drivers for cron expressions and RRULE patterns, and you pick which libraries to install:

# For cron expressions
composer require dragonmantank/cron-expression
 
# For RRULE (pick one)
composer require rlanvin/php-rrule
# or
composer require simshaun/recurr

A cron-based schedule is straightforward:

use DirectoryTree\Cadence\Drivers\CronSchedule;
 
// Bill this subscription every month on the 1st at midnight
$subscription->addSchedule(new CronSchedule('0 0 1 * *'));

For more expressive recurrence, RRULE gives you a lot more control over things that are awkward to express in cron:

use DirectoryTree\Cadence\Drivers\RruleSchedule;
 
// Every two weeks on Tuesday and Thursday
$subscription->addSchedule(new RruleSchedule('FREQ=WEEKLY;INTERVAL=2;BYDAY=TU,TH'));

Schedules are timezone-aware, so users in different regions get the right local time:

$subscription->addSchedule(
new CronSchedule('0 8 * * 1', 'Australia/Sydney') // Monday 8am Sydney time
);

Dispatching and Reacting

Cadence provides a schedules:run Artisan command that you register with Laravel's scheduler to run every minute:

// routes/console.php
Schedule::command('schedules:run')->withoutOverlapping()->everyMinute();

Each time it runs, it finds all records where next_run_at is past due, fires a ScheduleTriggered event for each one, and advances the next occurrence. Your application reacts through a listener:

use DirectoryTree\Cadence\Events\ScheduleTriggered;
 
class ProcessDueSubscription
{
public function handle(ScheduleTriggered $event): void
{
$subscription = $event->schedule->schedulable;
 
$subscription->processRenewal();
}
}

The event carries the schedule record, giving you access to the parent model, the expression, and timestamps for branching on model type or logging what ran and when.

Extensibility

If neither cron nor RRULE fits, you can implement a custom driver by extending the base Schedule class and implementing resolveNextOccurrence(), which receives a CarbonInterface and returns the next occurrence. Drivers are registered by name, so swapping or extending them later is straightforward.

You can learn more and view the source code on GitHub.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
SerpApi logo

SerpApi

Access real-time search engine results through a simple API—no more scraping headaches! Use it for AI applications, SEO tools, product research, travel information, and more

SerpApi
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift

The latest

View all →
Laravel's AI SDK adds sub-agents image

Laravel's AI SDK adds sub-agents

Read article
Laravel Introduces First-Party Passkey Authentication Support image

Laravel Introduces First-Party Passkey Authentication Support

Read article
Scrollbar Styling and Container Size Utilities in Tailwind CSS v4.3.0 image

Scrollbar Styling and Container Size Utilities in Tailwind CSS v4.3.0

Read article
Attach Addresses to Any Eloquent Model with Laravel Addressable image

Attach Addresses to Any Eloquent Model with Laravel Addressable

Read article
Generate Livewire Skeleton Placeholders Automatically with Wirebones image

Generate Livewire Skeleton Placeholders Automatically with Wirebones

Read article
Laravel ClickHouse: A Full-Featured ClickHouse Driver for Laravel image

Laravel ClickHouse: A Full-Featured ClickHouse Driver for Laravel

Read article