Yes, you can use Laravel's scheduler to automatically delete data from the database based on an expiry date. Here's an example of how you can do it:
- Create a new console command using the
make:commandArtisan command:
php artisan make:command DeleteExpiredData
- Open the
app/Console/Commands/DeleteExpiredData.phpfile and update thehandlemethod to delete the expired data:
public function handle()
{
DB::table('your_table')
->where('expiry_date', '<', now())
->delete();
}
Replace your_table with the name of your database table and expiry_date with the name of the column that contains the expiry date.
- Open the
app/Console/Kernel.phpfile and add a new entry to theschedulemethod to run the command daily:
protected function schedule(Schedule $schedule)
{
$schedule->command('delete:expired-data')->daily();
}
Replace delete:expired-data with the name of your console command.
- Save the files and run the following command to start the scheduler:
php artisan schedule:run
This will run the scheduler every minute and execute the DeleteExpiredData command once a day to delete the expired data.
Note: Make sure that your server's timezone is set correctly in the config/app.php file to ensure that the scheduler runs at the correct time.