<?phpnamespaceApp\Console\Commands;useIlluminate\Console\Command;classDemoCronextendsCommand{/** * The name and signature of the console command. * * @varstring */protected $signature ='demo:cron';/** * The console command description. * * @varstring */protected $description ='Command description';/** * Create a new command instance. * * @returnvoid */publicfunction__construct() {parent::__construct(); }/** * Execute the console command. * * @returnmixed */publicfunctionhandle() {\Log::info("Cron is working fine!");/* Write your database logic we bellow: Item::create(['name'=>'hello new']); */ }}
C:\xampp82\htdocs\testvn\app\Console\Kernel.php
<?phpnamespaceApp\Console;useIlluminate\Console\Scheduling\Schedule;useIlluminate\Foundation\Console\Kernelas ConsoleKernel;classKernelextendsConsoleKernel{/** * The Artisan commands provided by your application. * * @vararray */protected $commands = [Commands\DemoCron::class, ];/** * Define the application's command schedule. * * @param\Illuminate\Console\Scheduling\Schedule $schedule * @returnvoid */protectedfunctionschedule(Schedule $schedule) { $schedule->command('demo:cron')->everyMinute(); }/** * Register the commands for the application. * * @returnvoid */protectedfunctioncommands() {$this->load(__DIR__.'/Commands');requirebase_path('routes/console.php'); }}
php artisan schedule:run
Laravel 8 Cron Job Task Scheduling Tutorial
By Hardik Savani April 10, 2023 Category : Laravel
This tutorial is focused on laravel 8 cron job setup. i explained simply step by step laravel 8 task scheduling. This tutorial will give you simple example of how to create cron job in laravel 8. This article goes in detailed on how to make a cron job in laravel 8. So, let's follow few step to create example of task scheduling laravel 8 example.
Why we have to use cron job? and what is benefit to use cron jobs in laravel 8 and how to setup cron job in laravel 8?, If you have this question then i will explain why. Many times we need to send notifications or send email automatically to users for update property or products. So at that time you can define some basic logic for each days, hours etc can run and send email notification.
You also want to setup cron job on your server then you can follow this tutorial and let's implement it.
Step 1: Install Laravel 8
In this step, if you haven't laravel 8 application setup then we have to get fresh laravel 8 application. So run bellow command and get clean fresh laravel 8 application.
composer create-project --prefer-dist laravel/laravel blog
Step 2: Create New Command
In this step, we need to create our custom command. custom command will execute with task scheduling scron job. so, let's run bellow command to create new custom command.
<?php namespace App\Console\Commands; use Illuminate\Console\Command; class DemoCron extends Command{ /** * The name and signature of the console command. * * @var string */ protected $signature = 'demo:cron'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { \Log::info("Cron is working fine!"); /* Write your database logic we bellow: Item::create(['name'=>'hello new']); */ }}
In this step, we need to define our commands on Kernel.php file with time when you want to run your command like as bellow functions:
app/Console/Kernel.php
<?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule;use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel{ /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ Commands\DemoCron::class, ]; /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { $schedule->command('demo:cron') ->everyMinute(); } /** * Register the commands for the application. * * @return void */ protected function commands() { $this->load(__DIR__.'/Commands'); require base_path('routes/console.php'); }}
Step 4: Run Scheduler Command For Test
now we are ready to run our cron, so you can manually check using following command of your cron. so let's run bellow command:
php artisan schedule:run
After run above command, you can check log file where we already print some text. so open you your log file it looks like as bellow:
storage/logs/laravel.php
[2019-04-24 03:46:42] local.INFO: Cron is working fine! [2019-04-24 03:46:52] local.INFO: Cron is working fine! [2019-04-24 03:46:55] local.INFO: Cron is working fine!
At last you can manage this command on scheduling task, you have to add a single entry to your server’s crontab file: