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

johnnyhalford93's avatar

Delete data after expired date?

I have 3 tables.

Products

ID NAME PRICE

1 JACKET 50

Sales

ID NAME DATE_SALE

11 CHRISTMAS 06/19/2019

Sale_Product(Pivot table)

ID Product_id Sale_id Discount

1 1 11 25

I wanna delete this row in Sale_Product after value of DATE_SALE on table Sales expired.

Does anybody have an idea, how to do that?

0 likes
10 replies
johnnyhalford93's avatar

I've done with schedule. This works only when I execute command php artisan schedule:run. Is there any other solution to run this task automatically, without typing command?

johnnyhalford93's avatar

It's not working.

Here's my code.

My Commands file (DeleteOldSales.php)

                use Illuminate\Console\Command;
                use Illuminate\Support\Facades\DB;
                use Carbon\Carbon;
                use App\Sale;


                class DeleteOldSales extends Command
              {

                 protected $signature = 'command:deleteoldsales';


                protected $description = 'Command description';

                public function __construct()
                {
                 parent::__construct();
                }

                public function handle()
               {
                 $sales = DB::table('sale_product')
                       ->join('sales', 'sales.id', '=', 'sale_product.sale_id')
                       ->where('sales.date_sale','<', Carbon::now()->subMinutes(10))->delete();
                       echo "DeleteOldSales has been send successfully";
               }
              }

My Kernel.php

                 class Kernel extends ConsoleKernel
                 {

                     protected $commands = [
                              Commands\DeleteOldSales::class
                              ];


                     protected function schedule(Schedule $schedule)
                       {
                        $schedule->command('command:deleteoldsales')
                                              ->hourly();
 
                      }
                       protected function commands()
                      {
                      $this->load(__DIR__.'/Commands');

                      require base_path('routes/console.php');
                   }
Cronix's avatar

Did you set the cron job up, and are you sure it's running?

The cron job isn't something you can set via laravel. The scheduled jobs/tasks to be run are, but the cron job itself that runs every minute is set at the server level.

https://en.wikipedia.org/wiki/Cron

johnnyhalford93's avatar

Maybe because I use win 10, I couldn't do that. Even if I did all things correctly.

jlrdw's avatar

Are you in development. If later your actual site will be on Linux, I would consider setting up a Linux development environment. Just a 2 cents.

johnnyhalford93's avatar

I've solved.

The bottom line, when we are using win10 for task scheduling, we should guide to https://quantizd.com/how-to-use-laravel-task-scheduler-on-windows-10/.

But, I've had my command. So, in Add Arguments field I have to fill e.g. C:\xampp\htdocs\blogbits\artisan command:name instead, not schedule:run.

Note*** This is only when we previously create command which we later call in Kernel.php.

Otherwise, when we don't have our command. This link is appropriate.

Please or to participate in this conversation.