In Laravel, you can use the built-in task scheduling feature to set up cron jobs. Cron jobs allow you to schedule and automate recurring tasks in your Laravel application. Here's how you can set up cron job task scheduling in Laravel:
Step 1: Install Laravel App
composer create-project laravel/laravel example-pagination
Step 2: Create New Command
php artisan make:command DemoCron --command=demo:cron
Add Changes in below file,
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(): void
{
info("Cron Job running at ". now());
/*------------------------------------------
--------------------------------------------
Write Your Logic Here....
I am getting users and create new users if not exist....
--------------------------------------------
--------------------------------------------*/
$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
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:
ex. everyMinute(),everyFiveMinutes(),everyFifteenMinutes(),hourly(),daily(),dailyAt(’13:00′),twiceDaily(1, 13), weekly(),weeklyOn(1, ‘8:00’),monthly(),monthlyOn(4, ’15:00′),quarterly(),yearly(),timezone(‘America/New_York’).
app/Console/Kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*/
protected function schedule(Schedule $schedule): void
{
$schedule->command('demo:cron')
->everyMinute();
}
/**
* Register the commands for the application.
*/
protected function commands(): void
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Step 4: Run Scheduler Command For Test
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
[2022-03-01 03:51:02] local.INFO: Cron Job running at 2022-03-01 03:51:02
[2022-03-01 03:52:01] local.INFO: Cron Job running at 2022-03-01 03:52:01
[2022-03-01 03:53:02] local.INFO: Cron Job running at 2022-03-01 03:53:02
Laravel 10 Cron Job Setup on Server
Here, i will show you how to setup cron job command on server. you need to install crontab on server. if you are using ubuntu server then it already installed. so let's run bellow command and add new entry for cron job.
crontab -e
Now, add bellow line to crontab file. make sure you need to set your project path correctly on it.
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Now, you can check on server ,Thank You