04
MayIn this blog, we are going to introduce our Laravel Recurring package - the ultimate solution for adding recurring functionality to your Laravel Models! Whether you need simple daily, weekly, or everyday recurrence, or more complex patterns like repeating a model on the second Friday of every month, our package has got you covered. With seamless integration into your Laravel application, you can easily manage and automate recurring tasks with just a few lines of code.
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
1. Adding the recurring functionality to Models:
Make sure your models implements Repeatable Contract.
use MohammedManssour\LaravelRecurringModels\Contracts\Repeatable as RepeatableContract;
class Task extends Model implements RepeatableContract
{
}
2. Add the Repeatable trait to your Model
use MohammedManssour\LaravelRecurringModels\Contracts\Repeatable as RepeatableContract;
use MohammedManssour\LaravelRecurringModels\Support\Repeatable;
class Task extends Model implements RepeatableContract
{
use Repeatable;
}
3. Optionally, customize Model base date.
/**
* define the base date that we would use to calculate repetition start_at
*/
public function startsAt(): Carbon
{
return $this->created_at;
}
The Repeatable trait has a repeat method that will help you define the recurrence rules for the model.
The repeat method will return the 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 startsAt
returned value.
2. Every N Days Recurrence
This will help you to create every N Days recurring rule for the model.
$model->repeat()->everyNDays(days: 3)
The recurrence will start after n=3 days based on the startsAt
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 startsAt
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: '*',
weekday: '*'
)
use whereOccurresOn
scope to get models that occurred on a specific date.
Task::whereOccurresOn(Carbon::make('2023-05-01'))->get()
use whereOccurresBetween
scope to get the models that occurred between two specific dates.
Task::whereOccurresBetweeb(Carbon::make('2023-05-01'), Carbon::make('2023-05-30'))->get()
use endsAt to end occurrence on a specific date
$model->repeat()->daily()->endsAt(
Carbon::make('2023-06-01')
)
use endsAfter
to end occurrence after n times.
$model->repeat()->daily()->endsAfter($times);
startsAt
method to start occurrence after a specific date.$model->repeat()->daily()->startsAt(
Carbon::make()
)
Note
The team of Codebrisk Laravel developers is always ready to execute even your boldest ideas. Our expert team can design and develop any type of custom CRM solution, SAAS app, or e-commerce app to meet our customer’s needs and transform our customer’s experiences. Get in touch with our team to discuss your bespoke ideas and learn more about the next steps to launching cooperation.
Published at : 04-05-2023
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project