skip to Main Content

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

2

Answers


  1. This is how I would do

    Create a command

    php artisan make:command DeleteSchedule
    
    <?php
    // AppConsoleCommandsDeleteSchedule
    protected $signature = 'deleteschedule';
    
    public function handle()
    {
        Model::where('created_at', '<', now()->subMonths(18))
            ->delete();
    }
    

    Run a command every 1 day

    // AppConsoleKernel
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('deleteschedule')
            ->daily();
    }
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search