Cron Job in Laravel | How to Setup Cron Job ?
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:testphp artisan make:model Item -mNext 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 migrateNext 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:workNow successfully data inserted in database.
Thanks for reading 👍👍.
