Search code examples
laravellaravel-4delete-row

Laravel Remove All Records One Week Old


I want to create a cron job which fires off a task/command in Laravel to remove all records which are 1 week old. I am trying to use ExpressiveDate to help me out to evaluate the time. What is the best way in Laravel to do this?

I have registered the command:

Artisan::add(new removeWeek);

And created a removeWeek.php command under /apps/command and that works fine. I am looking for the best syntax -an eloquent way- to traverse my records and only remove records a week old from the current day. The cron job will run once a day.


Solution

  • Looks like you have all set, so now you just have to

    Calendar::where('created_at', '<', \Carbon\Carbon::now()->subWeek())->delete();
    

    One last thing: classes names are conventionally Capitalized:

    class RemoveWeek() {
    
    }