Scheduling Tasks Made Simple with Laravel on TheDevNerd

 Task Scheduling in Laravel 7

Task Scheduling Laravel - StudyWithKishan

In this article we will learn how to implement Task Scheduling in Laravel 7 with example . Task Scheduling makes it easier when you want to perform a perticular task or job at a perticular inerval of time like sending mothly reports to your user or sending payment alerts every month etc .

Here we will make a scheduler which will create a dummy user in every one minute using factory in our user table .

So let's dive in and see how to use task scheduling in Laravel .

Table of Content :

  • Create an Artisan Command
  •  Write Your Task
  • Registrer in kernel
  • Run your command

Step 1 - Create an Artisan Command :

To work on Task Scheduling , at first create you own artisan command using the following command .

php artisan make:command command_name

For example i'm creating a command named adduser .

php artisan make:command adduser

This command will create a file named adduser.php inside app/Console/Commands/adduser.php .

Step 2 - Write Your Task :

Now we need to write our task inside adduser.php . Provide your command name and description of the command as shown below and write your task logic inside the handle() method .

adduser.php :

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\User;
class AddUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'adduser';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This command will add a new dummy user in users table';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Factory(User::class,1)->create();
    }
}

Step 3 - Register in Kernel.php :

Now we need to register our artisan command inside app/Console/Kernel.php .  Inside Kernel.php uncomment the line present inside the schedule function and provide your own command name and time duration as shown below .

<?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 = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('adduser')->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

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

Visit the official site to get more time methods to use Laravel task scheduling time methods

Step 4 - Run Your Command :

Now you are all set , just run your command as shown below and you will see a new user will be created in every minute in your table .

php artisan adduser

Task Scheduling Laravel - StudyWithKishan


Previous Post Next Post

Contact Form