skip to Main Content

Laravel How to delete database data after 5 minutes of adding?

Schema::create('listings', function (Blueprint $table) {
  $table->id();
  $table->string('name');
  $table->string('country');
  $table->string('date');
  $table->timestamps();
});

2

Answers


  1. You can use cron job for this. You have to set cron job time to execute after 5 minutes and removed database which was created before 5 minutes.

    Please refere:-
    https://laravel.com/docs/9.x/scheduling

    Login or Signup to reply.
  2. Just use this in the scheduler.(or can call a event)

    This gets the record for the last 5 minutes and deletes the data.

    $minutes  = Carbon::now()->subMinutes( 5 );
    Listing::where('created_at', '<=', $minutes)->delete();
    

    And schedule it to everyMinute()


    Note: I assumed you have a model called Listing or change according to it.

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