Home > Software engineering >  Custom schedule deletion in Laravel
Custom schedule deletion in Laravel

Time:01-18

How can it be done after you set the expiration time of the record, it will be automatically deleted. After you set a specific duration older than 1 year or 6 months or 15 days it will automatically delete the record. What do I need?

enter image description here

CodePudding user response:

This is how I would do

Create a command

php artisan make:command DeleteSchedule
<?php
// App\Console\Commands\DeleteSchedule
protected $signature = 'deleteschedule';

public function handle()
{
    Model::where('created_at', '<', now()->subMonths(18))
        ->delete();
}

Run a command every 1 day

// App\Console\Kernel
protected function schedule(Schedule $schedule)
{
    $schedule->command('deleteschedule')
        ->daily();
}

CodePudding user response:

I would not use commands, because you simply run it once a day, and dont delete records immediately. This may cause issues, such as displaying old records of that same day, and processing them, if system is using these records to do something afterwards. Even statistics of these records would not be accurate all the time.

I would personally use jobs, and schedule them to run at the time record expires. This way record will be deleted at the same time they expire.

  • Related