Cron Job in Laravel | How to Setup Cron Job ?

Amit Kumar
2 min readJan 21, 2025

--

In This tutorial we’re going to learn how to use Cron job in Laravel with using schedule command I have to inserted every 5 seconds data in database using Cron job.

php artisan make:command CronJob --command=cron:test
php artisan make:model Item -m

Next go to migration and put the below code.

public function up(): void
{
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->string('item_name');
$table->string('specs');
$table->timestamps();
});
}

Next go to Item model and put the below code
C:\xampp\htdocs\learning\cron_jb\app\Models\Item.php

protected $guarded = [];

Next to migrate the table using below command.

php artisan migrate

Next go to and put there condition :- C:\xampp\htdocs\learning\cron_jb\app\Console\Commands\CronJob.php

public function handle()
{
Item::create([
'item_name'=>'Hi testing Item',
'specs'=>'Our testing specs',
]);
}

Next go to C:\xampp\htdocs\learning\cron_jb\app\Console\Kernel.php

 protected $command = [
Commands\CronJob::class,
];

protected function schedule(Schedule $schedule): void
{
$schedule->command('cron:test')->everyFiveSeconds();
}

Run below command for test cron job

php artisan schedule:work

Now successfully data inserted in database.

Thanks for reading 👍👍.

--

--

Amit Kumar
Amit Kumar

Written by Amit Kumar

Hi I am Amit Experienced Web Developer with a demonstrated history of working in the information technology and services industry.

No responses yet