Automate Tasks in Laravel 12 Using the Scheduler

Automate Tasks in Laravel 12 Using the Scheduler

Laravel is known for its elegant syntax and developer-friendly capabilities and the Laravel task scheduling tool is no different. With the release of Laravel 12, automating repetitive tasks like sending emails, cleaning logs, or generating reports has become even more streamlined using Laravel’s built-in scheduler.

Laravel Development Services

The Laravel Scheduler is an advanced way to fluently and expressively define command based tasks in your application. Instead of creating a separate crontab for each of your tasks, Laravel allows you to define all of your scheduled tasks in one place; app/Console/Kernel.php.

In this blog, we will learn scheduling tasks that need to be run at certain time intervals.

Prerequirement:

1. Composer (latest Version)

2. Laravel version 12

Steps to Automate Tasks in Laravel 12 using Scheduler:

Step 1: Create a Project in Laravel 12

composer create-project laravel/laravel:^12.0 task-scheduler
cd task-scheduler

Step 2: Set Up the Database & Model

php artisan make:model User -m

Open the migration file and modify it

Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamp('last_active_at')->nullable();
    $table->timestamps();
});

Now, migrate the table by the following command:

php artisan migrate

As appropriate, add few records into the database as shown below

'name' => 'Test User',
'email' => 'user@gmail.com',
'last_active_at' => Carbon::now(), // Active now
'name' => 'Bob Inactive',
'email' => 'bob@example.com',
'last_active_at' => Carbon::now()->subMonths(2), // Inactive

Step 4: Create a Custom Artisan Command

Now, run the following command to create a custom command.

php artisan make:command ClearInactiveUsers

Edit app/Console/Commands/ClearInactiveUsers.php

protected $signature = 'users:clear-inactive';
protected $description = 'Delete users inactive for over 1 year';
public function handle()
{
    $deleted = \App\Models\User::where('last_active_at', '<', now()->subYear())->delete();
    $this->info("Deleted {$deleted} inactive user(s).");
}

Open app/Console/Kernel.php and register the command:

protected $commands = [
    Commands\ClearInactiveUsers::class,
];

Step 5: Edit the routes/console.php file

Inside the routes/console.php file, you can add the code as given below

Schedule::command('users:send-inactive')
    ->daily()
    ->withoutOverlapping();

Step 6: Testing the Scheduler Locally

You can manually test it before scheduling it using the command below

php artisan users:clear-inactive

Output:

You will get output similar to

Deleted 1 (based on your records) inactive user(s).

Conclusion

Laravel’s task schedule provides a clean and expressive way to automate recurring tasks and lead you to a correct location right within your application code.

Hire laravel Developer

Automating tasks is essential to maintain a healthy and functioning Laravel application. With the Scheduler in Laravel 12, you can funnel your cron-based tasks into one location, while being expressive and maintainable.

Happy Coding!

Previous Article

Creating a Custom Mega Menu in Shopify Without an App: A Step-by-Step Guide

Next Article

Advanced Debugging and Monitoring with Laravel Telescope

View Comments (1)

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨