Laravel 11 Cron Job Task Scheduling Example

Websolutionstuff | Jan-02-2025 | Categories : Laravel

Hi there! You're in the right place if you’ve ever wanted to automate repetitive tasks in your Laravel application. Laravel 11 makes it super simple to schedule tasks with its built-in task scheduling system. Whether it’s sending daily emails, clearing logs, or syncing data, you can handle it without messing around with traditional cron job files.

In this guide, I’ll walk you through how to create and set up cron jobs in Laravel 11. By the end of this tutorial, you’ll have a solid understanding of how to automate tasks efficiently in your application.

Laravel 11 Cron Job Task Scheduling Example

Laravel 11 Cron Job Task Scheduling Example

 

Step 1: Install Laravel 11

In this step, we'll install the laravel 11 application using the following command.

composer create-project laravel/laravel example-app

 

Step 2: Create a New Command

Next, we'll create a command using the following command.

php artisan make:command DemoCron --command=demo:cron

app/Console/Commands/DemoCron.php

<?php
  
namespace App\Console\Commands;
  
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use App\Models\User;
  
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';
  
    /**
     * Execute the console command.
     */
    public function handle()
    {
        info("Cron Job running at ". now());
      
        $response = Http::get('https://jsonplaceholder.typicode.com/users');
          
        $users = $response->json();
      
        if (!empty($users)) {
            foreach ($users as $key => $user) {
                if(!User::where('email', $user['email'])->exists() ){
                    User::create([
                        'name' => $user['name'],
                        'email' => $user['email'],
                        'password' => bcrypt('123456789')
                    ]);
                }
            }
        }
    }
}

 

Step 3: Register as Task Scheduler

Then, we'll register the command in the console.php file.

routes/console.php

<?php
  
use Illuminate\Support\Facades\Schedule;
  
Schedule::command('demo:cron')->everyMinute();
 
Step 4: Run Scheduler Command

Now, run the scheduler using the following command.

php artisan schedule:run

 

Step 5: Laravel Cron Job Setup on Server (Optional)

Next, we'll set cronjob on the server. You need to install crontab on the server. If you are using Ubuntu Server, then it is already installed.

crontab -e

Now, add the following line to the crontab file.

* * * * * cd /path-to-your-project & php artisan schedule:run >> /dev/null 2>&1

 


You might also like:

Recommended Post
Featured Post
How to Integrate Cashfree Payment Gateway in Laravel 10
How to Integrate Cashfree Paym...

Hello developers! Today, we're about to embark on a journey to elevate our Laravel applications by integrating...

Read More

Feb-12-2024

Laravel 9 Left Join Query Example
Laravel 9 Left Join Query Exam...

In this article, we will see the laravel 9 left join query example. laravel 9 left join eloquent returns all rows from t...

Read More

Mar-31-2022

How To Validate Max File Size Using Javascript
How To Validate Max File Size...

This article will show us how to validate max file size using javascript. Many times we have a requirement to check...

Read More

Aug-03-2020

How to Run Specific Seeder in Laravel 8
How to Run Specific Seeder in...

In this example, we will learn how to run a specific seeder in laravel 8. If you want to run only one seeder in laravel...

Read More

Jan-19-2022