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?
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');
}
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.