Laravel Cron Jobs With Task Scheduler Example

Hey Artisan are you looking for Laravel Cron Job tutorial ? In this Laravel Cron Jobs tutorial i will show it in localhost. So in laravel cron job in localhost tutorial i create a simple laravel custom command for cron job and we will run our cron job in every minute.

In this laravel custom cron job or task scheduling example tutorial i will show you daily created users using laravel cron job with laravel task scheduler. Laravel's command scheduler allows us to fluently and expressively define our custom command schedule within Laravel itself. Using the laravel task scheduler, only a single Cron entry is needed on your server. 

If you still don't know what is task scheduler and cron job and how to setup it in localhost or cpanel or live server, then you are a right place. Task scheduling is a system where you can run a method after a couple of minute, second or hour or whatever you like to set up it. 

 Our task schedule is defined in the app/Console/Kernel.php file's schedule method. To help you get started, a simple example is given below to better understand. 

 

Step 1:  Create Command Class

In this step we need to create custom command to check our laravel cron job example in localhost. So run below command to check it.

php artisan make:command ourCustomCommandName 
php artisan make:command NewUserCreated

Having run this command you will find this file in the following directory app/Console/Commands/NewuserCreated.php 

 

Step 2:  Setup app\Console\Commands\NewUserCreated.php

Now open newly created app/Console/Commands/NewuserCreated.php write this below logic to get daily users. 

app/Console/Commands/NewUserCreated.php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class NewUserCreated extends Command
{
    
    protected $signature = 'todaysuser:test';

    protected $description = 'Todays user';

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

    public function handle()
    {
        
        $count = \DB::table('users')
        ->whereDate('created_at', '=', date('Y-m-d'))
        ->count();

        dd("Todays {$count}");
    }
}

Here in handle() method we've print daily created users, nothing else. So when we run our custom command it will show us daily created users.

 

Step 3: Register Command

Now we have to register our command to console file. So open it and register it like below.

app/Console/Kernel.php

namespace App\Console;

use App\Console\Commands\NewUserCreated;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
   
    protected $commands = [
        Commands\NewUserCreated::class,
    ];

  
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();
        $schedule->command('todaysuser:test')
                 ->everyMinute();
    }

  
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

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

 

There are many options to run scheduler. You can check it from Laravel task scheduling documentation.

 

Step 4: Test Scheduling

Now time to check our Laravel Cron Job tutorial. Everything are set to go. So now open your terminal and run below command.

php artisan todaysuser:test

Then you will able see the below output

Output
Todays 0

 

Now again run

php artisan tinker
//then
factory(\App\User::class,1)->create()
//then
exit
//
php artisan todaysuser:test

Then you should have seen the below output.

Output
Todays 1

 

This is the preview image of our laravel cron job tutorial. If you run those command then you should see this image.

 

Read also : Send Notification to Inactive User with Task Scheduling in Laravel using Custom Command

 

Hope you have enjoy this laravel cron job or task scheduler tutorial.

 

#laravel #cron-job #task-scheduler #custom-commands #laravel-7 #laravel-6