Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Nihir's avatar
Level 50

How can delete the record after the created date is gone

Hi, I worked on Hotel Management Project. I describe the slot based on date and how can I delete that slot automatically after the date is gone in laravel 9.

0 likes
13 replies
ga46's avatar

create your own function, and add your delete logic in this function then create an active route and add a cronjob to this route.

Nihir's avatar
Level 50

@Laralex I tried scheduler but I have no idea how to implement it in the server.

Nihir's avatar
Level 50

@ga46 @laralex I tried the simple thing that whenever a specific route was called, the query ran automatic and displayed the data according to my logic like this in function

Slot::where('created_at','<',date('Y'-'m'-'d'))->delete();

Is it worth It or not?

Snapey's avatar

are not ALL records created in the past?

can't you run the scheduler on your server? Having a route to do this is such a hack

Nihir's avatar
Level 50

Hi @Snapey I test this query in my local and XAMPP PHP admin, and its works properly. After some tests, I did it on the server As a temporary solution.

But I don't know about the perfect solution or TaskSheduler For the server; this is a temporary solution.

Please let me know if you have any good and easy suggestions or tutorial blogs for this.

Snapey's avatar

@Nihir but i don't understand your query. Why not just delete every record?

Nihir's avatar
Level 50

@Snapey, I just deleted the record that record which created_At date is gone means created_at date is past dates

Snapey's avatar

@Nihir but created_at is ALWAYS past dates. When is created_at in the future?

Nihir's avatar
Level 50

@Snapey In my case, this field is custom for set future date.

lat4732's avatar

@nihir If you are on a local server (XAMPP) you can start scheduler with php artisan schedule:work. In production (live server) you must setup a cron job that runs every minute (* * * * *)

/usr/local/bin/php /home/youruser/public_html/artisan schedule:run >> /dev/null 2>&1

Scheduler tasks are inside app/Console/Kernel.php and you can create a new one like this:

$schedule->call(function () {
         $expired = Slot::where('created_at', '<', now())->get();
         foreach($expired as $slot) {
               $slot->delete();
         }
})->daily()->name('Delete expired slots');

Please or to participate in this conversation.