How to Setup Dynamic Cron Job(Task Scheduling) in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

This article will provide example of laravel dynamic task scheduling. This article will give you simple example of laravel dynamic cron job setup. This tutorial will give you simple example of laravel dynamic scheduler example. I’m going to show you about laravel add dynamically task scheduler.

Here you can simply set up cron dynamic task scheduling in laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10.

Sometime we need to setup dynamically cron job for our laravel project. you might have to create master to admin can set daily, weekly, monthly or specific time for task scheduler. so you can do it using laravel task scheduler.

I will show you step by step how to set dynamically cron jon using laravel task scheduler. but if you want to add cron job with daily, weekly etc without dynamic then you can also read my this article, Setup Cron Job in Laravel.

Let's see bellow step to setup cron job dynamic in laravel.

Step 1: Install Laravel

In this step, if you haven't laravel application setup then we have to get fresh laravel 7 application. So run bellow command and get clean fresh laravel 7 application.

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create Migration

Here, we need create database migration for tasks table and also we will create model for Task table.

php artisan make:migration create_tasks_table

Migration:

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class CreateTasksTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('tasks', function (Blueprint $table) {

$table->id();

$table->string('title')->nullable();

$table->string('type')->nullable();

$table->string('frequency')->nullable();

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('tasks');

}

}

php artisan migrate

Step 3: Create Model

now we will create Task model by using following command:

php artisan make:model Task

App/Task.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Task extends Model

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'title', 'type', 'frequency',

];

}

Step 4: Create Seeder

Now, we will create seeder for adding some dummy records to tasks module. so let's create seeder and sun it.

php artisan make:seeder CreateTask

database/seeds/CreateTask.php

<?php

use Illuminate\Database\Seeder;

use App\Task;

class CreateTask extends Seeder

{

/**

* Run the database seeds.

*

* @return void

*/

public function run()

{

$tasks = [

[

'title' => 'It will run every minute',

'type' => 'everyMinute',

'frequency' => 'everyMinute'

],

[

'title' => 'It will run every five minute',

'type' => 'everyFiveMinutes',

'frequency' => 'everyFiveMinutes'

],

[

'title' => 'It will run daily',

'type' => 'daily',

'frequency' => 'daily'

],

[

'title' => 'It will run every month',

'type' => 'monthly',

'frequency' => 'monthly'

]

];

foreach ($tasks as $key => $task) {

Task::create($task);

}

}

}

Now, let's run created seeder by following command:

php artisan db:seed --class=CreateTask

Step 5: Set Dynamic Task Scheduling

Now, in this step, we will set dynamic cron job logic in Kernel.php file. so let's add as like bellow file.

app/Console/Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;

use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

use App\Task;

use Log;

class Kernel extends ConsoleKernel

{

/**

* The Artisan commands provided by your application.

*

* @var array

*/

protected $commands = [

'App\Console\Commands\DatabaseBackUp'

];

/**

* Define the application's command schedule.

*

* @param \Illuminate\Console\Scheduling\Schedule $schedule

* @return void

*/

protected function schedule(Schedule $schedule)

{

$schedule->command('database:backup')->daily();

/* Get all tasks from the database */

$tasks = Task::all();

foreach ($tasks as $task) {

$frequency = $task->frequency;

$schedule->call(function() use($task) {

/* Run your task here */

Log::info($task->title.' '.\Carbon\Carbon::now());

})->$frequency();

}

}

/**

* Register the commands for the application.

*

* @return void

*/

protected function commands()

{

$this->load(__DIR__.'/Commands');

require base_path('routes/console.php');

}

}

Now we are ready to setup our scheduler command on crontab. let's add command on crontab file on server.

crontab -e

add following command to that file with your project root path:

  

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

Now, it's runs automatically,

you can check output in your log file as like bellow:

I hope it can help you...

Tags :
Shares