In this tutorial, we will explore how to create a Cron Job in Laravel. Automating tasks with Cron Jobs in Laravel is simple and efficient. We’ll go through the entire process step by step, allowing you to understand task scheduling in Laravel thoroughly.
By the end of this tutorial, you’ll be able to grasp all the essentials of setting up Cron Jobs in Laravel and effortlessly automate various recurring tasks. As a web developer, you’re likely familiar with the hassle of repetitive tasks. This tutorial aims to simplify your life by automating tasks like:
- Managing App Backups
- Sending Offer-related Emails
- Database Optimization
- Creating Product Reports
Automating these tasks not only enhances productivity but also frees up time for more critical responsibilities. Let’s dive into creating Cron Jobs with Laravel’s Task Scheduling.
Laravel 9 Cron Job Scheduling Example
Laravel comes equipped with a powerful built-in task manager, allowing you to delegate tasks effectively. For this tutorial, it’s preferable to have a Linux operating system for managing Cron Jobs.
Building a New Application
If you don’t have an existing Laravel application, create a new one using the following command:
composer create-project laravel/laravel laravel-cron-job --prefer-dist
Navigate to the project directory:
cd laravel-cron-job
Configure Database Connection
Before proceeding, configure the database connection in your .env
file with your database details. Here’s an example for a MySQL database:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=
If you’re using the MAMP local server on macOS, add the following lines to specify the UNIX_SOCKET and DB_SOCKET:
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Create a New Laravel Artisan Command
To create a new artisan Laravel command, use the make:console
Artisan command. In this tutorial, we’ll demonstrate how to send a new daily quote. Run the following command:
php artisan make:command DailyQuote
This command generates a new artisan command, and you can find the code in the DailyQuote.php
file within the app/Console/Commands
folder.
// app/Console/Commands/DailyQuote.php namespace App\Console\Commands; use Illuminate\Console\Command; class DailyQuote extends Command { protected $signature = 'quote:daily'; protected $description = 'Respectively send an exclusive quote to everyone daily via email.'; public function __construct() { parent::__construct(); } public function handle() { // Your code to send daily quotes via email goes here $this->info('Successfully sent daily quote to everyone.'); } }
Register Task Scheduler Command
Now, let’s register the newly created Task Scheduler class in your Laravel application. Open the app/Console/Kernel.php
file and add the following code:
// app/Console/Kernel.php namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { protected $commands = [ Commands\DailyQuote::class, ]; protected function schedule(Schedule $schedule) { $schedule->command('quote:daily')->everyMinute(); } protected function commands() { $this->load(__DIR__.'/Commands'); require base_path('routes/console.php'); } }
In this code, we’ve injected the DailyQuote
command into the $commands
array, and we’ve scheduled it to run every minute using $schedule->command('quote:daily')->everyMinute();
.
You can view your custom artisan command by running:
php artisan list
You’ll see your command listed with its description.
Running the Laravel Scheduler
There are two ways to run the scheduler:
Manual Method:
You can manually run the scheduler using the following command:
php artisan schedule:run
This command will execute your scheduled tasks.
Automated Method (Recommended):
To automate the Laravel Scheduler, set up a Cron Job that runs every minute. SSH into your server, navigate to your project directory, and run:
crontab -e
Add the following line to the Crontab file (replace /path/to/artisan
with the full path to your Laravel project’s artisan command):
* * * * * cd /your-project-path && php artisan schedule:run >> /dev/null 2>&1
This Cron Job will ensure your Laravel Scheduler runs automatically every minute.
Summary
This tutorial has demonstrated the vast capabilities of Laravel Task Scheduler, allowing you to create custom commands and automate various tasks efficiently. Laravel excels in providing tools and features that make it a robust PHP framework for web development.
Task scheduling with Laravel’s Cron Jobs simplifies managing recurring tasks, enhances productivity, and streamlines your workflow. Explore the Laravel documentation for further insights into Laravel Scheduling Tasks.