Loading...

How to make a cronjob in Laravel

Image

Creating a cron job in Laravel to automate tasks such as caliing some api or doing some functionality automatically at a particular time or every fix time.

In laravel creating a cronjob is not a big thing as laravel already gives many commands and other options to create a cronjob. 

How cronjob works in laravel:

In order to automate some tasks we will create our logic and a command. In that command we will specify that at what time our command should run or after how many intervals our command should run , that we can mention in our cronjob command. The interval options are like everySecond , everyTenSeconds , everyMinute , everyTenMinutes , everyFifteenMinutes etccc like this or for more details you can visit Laravel cronjobs.

How to create a cronjob in Laravel:

Step 1: Create a New Command

Laravel allows you to define custom Artisan commands. Start by creating a new command for your cron job.

  1. Create a new Artisan command:

    php artisan make:command GenerateBlogPost
  2. Define the command logic:
    1. In the generated file located at app/Console/Commands/GenerateBlogPost.php, you can define the logic to create a blog post.

      <?php
      
      namespace App\Console\Commands;
      
      use Illuminate\Console\Command;
      use App\Models\BlogPost;
      use Illuminate\Support\Str;
      
      class GenerateBlogPost extends Command
      {
          protected $signature = 'generate:blogpost';
          protected $description = 'Generate a new blog post with SEO keywords';
      
          public function __construct()
          {
              parent::__construct();
          }
      
          public function handle()
          {
              // Define SEO keywords and description
              $keywords = [
                  'Laravel cron job', 'automate tasks in Laravel', 'SEO optimization', 
                  'Laravel commands', 'scheduling in Laravel'
              ];
              $description = 'Learn how to create and schedule a cron job in Laravel to automate tasks, including generating blog posts with SEO keywords.';
      
              // Create a new blog post
              BlogPost::create([
                  'title' => 'How to Create a Cron Job in Laravel',
                  'content' => $this->generateContent($keywords, $description),
                  'seo_keywords' => implode(',', $keywords),
                  'seo_description' => $description,
              ]);
      
              $this->info('Blog post created successfully!');
          }
      
          private function generateContent($keywords, $description)
          {
              // Generate content with keywords
              $content = "Creating a cron job in Laravel is a great way to automate tasks. "
                       . "In this tutorial, we'll cover the steps to set up a cron job, including the following keywords: "
                       . implode(', ', $keywords) . ".\n\n" 
                       . $description;
      
              return $content;
          }
      }
      

Step 2: Register the Command

Register the command in the app/Console/Kernel.php file.

protected $commands = [
    Commands\GenerateBlogPost::class,
];

protected function schedule(Schedule $schedule)
{
    $schedule->command('generate:blogpost')->daily();
}

At the place of daily() , we can specify which day ,which time , after what intervals should the command have to run,

After that you just have to setup cronjob for one time in your server or cpanel through which this command will run automatically. Also if you have already created one command and setted up on server for that domain than you don't need to set cronjob on server you can now just create as many command you want and set in the kernal file. It will automatically now call the command at your time.

How to use or call cronjob on local system in Laravel:

In order to call or test your cronjob on local system if you don't have any server than you just hit this command:

php artisan generate:blogPost //add your command after the artisan and it will be called

Thank you for reading!!!!

0 Comments

Leave a comment