Laravel provides an easy-to-use system for handling background tasks using Queues and Jobs. Queues allow you to defer the processing of a time-consuming task, such as sending emails or processing images, to be done in the background. This prevents the user from waiting for long processes to finish and allows the app to perform other tasks smoothly.
In Laravel, Jobs represent a task that needs to be executed, and Queues manage the execution of these jobs. When a job is dispatched to a queue, it is picked up by a worker, which processes the task in the background.
This tutorial will walk you through setting up and using Laravel Queues and Jobs for handling background tasks.
Make sure Laravel is installed and set up on your project. You can install Laravel via Composer:
composer create-project --prefer-dist laravel/laravel laravel-queues-example
By default, Laravel supports multiple queue backends like database, Redis, and others. For this example, we will use the database driver.
Open the .env
file and set the queue connection to database:
QUEUE_CONNECTION=database
Now, run the following command to create the necessary queue tables in the database:
php artisan queue:table
php artisan migrate
Jobs in Laravel represent tasks that need to be processed. To create a job, you can use the artisan command:
php artisan make:job SendWelcomeEmail
This command creates a new job file inside the app/Jobs
directory. The file will look like this:
namespace App\Jobs;
use Mail;
use App\Mail\WelcomeMail;
class SendWelcomeEmail extends Job
{
public function handle()
{
// Sending the welcome email
Mail::to('user@example.com')->send(new WelcomeMail());
}
}
In this example, we create a job to send a welcome email. The handle()
method contains the logic to be executed when the job is processed.
To send the job to the queue, we use the dispatch()
method. You can dispatch the job from any part of your application, such as a controller or service.
For example, in a controller:
use App\Jobs\SendWelcomeEmail;
public function register(Request $request)
{
// Validate and create user
$user = User::create($request->all());
// Dispatch the job to the queue
SendWelcomeEmail::dispatch($user);
return response()->json('Registration successful!');
}
In this example, when a user is registered, the SendWelcomeEmail
job is dispatched, but it is processed in the background by a worker.
To process jobs from the queue, we need to run the queue worker. Open a terminal and run:
php artisan queue:work
This command will listen for jobs in the queue and execute them as they are dispatched.
Laravel provides a built-in mechanism to handle failed jobs. If a job fails, it is retried or logged in the failed_jobs
table.
To create the necessary table, run:
php artisan queue:failed-table
php artisan migrate
In your job, you can define what should happen if it fails using the failed()
method:
public function failed(Exception $exception)
{
// Logic for failed job
}
SendWelcomeEmail::dispatch($user)->delay(now()->addMinutes(10));
config/queue.php
file.Here’s how the process works end-to-end:
SendWelcomeEmail
job).failed_jobs
table, and you can define retry or failure actions.Using Laravel 12 Queues and Jobs helps you efficiently handle background tasks and optimize user experience by offloading time-consuming tasks to be processed later. With Laravel's simple and powerful queue system, you can improve application performance and keep your application responsive.
By following the steps above, you can set up background task processing in no time and handle emails, notifications, image processing, and more efficiently in your Laravel application.