mohammedmanssour/
laravel-recurring-models
g4t.io

Create Repeatable Models with Laravel Recurring Models

17 views 268 stars by Paul Redmond

Adds Repeatable functionlity to Laravel Models

README from mohammedmanssour/laravel-recurring-models Open on GitHub →

Laravel Recurring Models

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Introducing our Laravel Recurring package - the ultimate solution for adding recurring functionality to your Laravel Models! Whether you need simple daily, weekly, or every n days recurrence, or more complex patterns like repeating a model on the second Friday of every month, our package has got you covered. With a seamless integration into your Laravel application, you can easily manage and automate recurring tasks with just a few lines of code.

Installation

You can install the package via composer:

composer require mohammedmanssour/laravel-recurring-models

You can publish and run the migrations with:

php artisan vendor:publish --tag="recurring-models-migrations"
php artisan migrate

Usage

Adding the recurring functionality to Models:

  1. Make sure you models implements Repeatable Contract.
use MohammedManssour\LaravelRecurringModels\Contracts\Repeatable as RepeatableContract;

class Task extends Model implements RepeatableContract
{

}
  1. Add the Repeatable trait to your Model
use MohammedManssour\LaravelRecurringModels\Contracts\Repeatable as RepeatableContract;
use MohammedManssour\LaravelRecurringModels\Concerns\Repeatable;

class Task extends Model implements RepeatableContract
{
    use Repeatable;
}
  1. Optionaly, customize Model base date.
/**
 * define the base date that we would use to calculate repetition start_at
 */
public function repetitionBaseDate(RepetitionType $type = null): Carbon
{
    return $this->created_at;
}

Model Recurring rules

The Repeatable trait has repeat method that will help you define the recurrence rules for the model.

The repeat method will return Repeat helper class that has 4 methods: daily, everyNDays, weekly, and complex

1. Daily Recurrence

This will help you to create daily recurring rule for the model.

$model->repeat()->daily()

The recurrence will start the next day based on the repetitionBaseDate returned value.

2. Every N Days Recurrence

This will help you to create every N Days recurring rules for the model.

$model->repeat()->everyNDays(days: 3)

The recurrence will start after n=3 days based on the repetitionBaseDate returned value.

3. Weekly Recurrence

This will help ypi create weekly recurrence rule for the model.

$model->repeat()->weekly()

The recurrence will start after 7 days based on the repetitionBaseDate returned value.

You can specify the days of the recurrence using the on method.

$model->repeat()->weekly()->on(['sunday', 'monday', 'tuesday'])

4. Complex Recurrence.

This will help you create complex recurrence rules for the task.

$model->repeat()
    ->complex(
        year: '*',
        month: '*',
        day: '*',
        week: '*',
        weekOfMonth: '*',
        weekday: '*'
    )
Examples
  1. Repeat model on the second friday of every month.
$model->repeat()->complex(weekOfMonth: 2, weekday: Carbon::FRIDAY)
  1. Repeat model on the 15th day of every month.
$model->repeat()->complex(day: 15)

Model Scopes

use whereOccurresOn scope to get models that occurres on a specific date.

Task::whereOccurresOn(Carbon::make('2023-05-01'))->get()

use whereOccurresBetween scope to get the models that occurres between two sepcific dates.

Task::whereOccurresBetween(Carbon::make('2023-05-01'), Carbon::make('2023-05-30'))->get()

1. End Recurrance

use endsAt to end occurrance on a specific date

$model->repeat()->daily()->endsAt(
    Carbon::make('2023-06-01')
)

use endsAfter to end occurrance after n times.

$model->repeat()->daily()->endsAfter($times);

2. Start Recurrance

use startsAt method to start occurrance after a specific date.

$model->repeat()->daily()->startsAt(
    Carbon::make()
)

Testing

composer test

Credits

License

The MIT License (MIT). Please see License File for more information.

More from the ecosystem

durable-workflow/
workflow
g4t.io
9 1,292

Durable Workflow: Long-Running Workflows for Laravel

Embedded durable workflow runtime and orchestration engine for Laravel applications.

durable-workflow/workflow via Laravel News
packstub/
session-replay
g4t.io
14 52

Session Replay for Laravel: Self-Hosted Browser Recordings

Session replay for Laravel: record what a person did in the browser with rrweb, keep the recording on your own disk and database, and watch it behind a gate you define — masked inputs by default, errors, failed Livewire requests and web vitals marked on the timeline, a link to the replay in every log line. packstub/filament-session-replay adds the sessions to a Filament panel.

packstub/session-replay via Laravel News
cyberwizard-dev/
schedule-catchup
g4t.io
17

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Schedule Catchup replays missed Laravel scheduled tasks after downtime, with opt-in recovery, bounded retries, and access to the original scheduled date.

cyberwizard-dev/schedule-catchup via Laravel News