While developing a personal project I was in the need to let user defines actions that run periodically.

My first thought was to use the Task Scheduling component of Laravel. The solution that I came up with is quite simple, yet effective.

In app/Console/Kernel.php inside the schedule method I placed this snippet of code.

if (Schema::hasTable('tasks')) {

// Get all tasks from the database

$tasks = Task::all(); // Go through each task to dynamically set them up.

foreach ($tasks as $task) {

// Use the scheduler to add the task at its desired frequency

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

// Place your logic here

})->cron($task->frequency);

}

}

I have a tasks table that stores the user generated tasks that run at the desired frequency.

The snippet of code is a sample to show you the idea, please account for scalability issues and corner cases.

What do you think? Is there a better way to implement this?