create your own function, and add your delete logic in this function then create an active route and add a cronjob to this route.
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.
@nihir Read about Laravel Scheduler. You can check for expired slots for example daily and delete them.
@Laralex I tried scheduler but I have no idea how to implement it in the server.
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
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.
@Nihir but i don't understand your query. Why not just delete every record?
@Snapey, I just deleted the record that record which created_At date is gone means created_at date is past dates
@Nihir but created_at is ALWAYS past dates. When is created_at in the future?
@Snapey In my case, this field is custom for set future date.
@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');
No, you don't have to use the for loop
https://laravel.com/docs/9.x/eloquent#deleting-models-using-queries
@frankielee I'm sure it doesn't matter how you do it but OK, you're right.
Please or to participate in this conversation.