Current Location: Home> Latest Articles> Guide to Using Laravel Queue Functionality: Configuration, Task Definition, and Execution

Guide to Using Laravel Queue Functionality: Configuration, Task Definition, and Execution

M66 2025-07-02

Introduction

Queue is a common asynchronous processing mechanism that plays an important role in web development. Laravel provides a powerful queue feature, which makes it easy to handle various background tasks, such as sending emails, generating reports, and processing large data. This article will explain how to use the queue functionality in Laravel, including queue configuration, task definition, and execution, with code examples.

Configuring the Queue

In the Laravel framework, configuring the queue is very simple. First, open the configuration file config/queue.php and set the queue driver and connection parameters. Laravel supports various queue drivers, such as database, Redis, Beanstalkd, etc. The following example demonstrates the use of the database driver:

<span class="fun">'connections' => [ 'database' => [ 'driver' => 'database', 'table' => 'jobs', 'queue' => 'default', 'retry_after' => 90, ], ],</span>

In this configuration, driver specifies that the database driver is used, table sets the name of the table where queue tasks will be stored, queue specifies the default queue name, and retry_after defines the retry time after a task fails.

Defining a Task

In Laravel, a queue task (Job) is defined as a class, usually stored in the app/Jobs directory. We will create a simple queue task to send an email.

First, use the Artisan command to generate the queue job template:

<span class="fun">php artisan make:job SendEmailJob</span>

After running the command, a class file named SendEmailJob will be created in the app/Jobs directory. Open this class file, and it will contain the following code:

<span class="fun">namespace AppJobs; use IlluminateBusQueueable; use IlluminateContractsQueueShouldQueue; use IlluminateFoundationBusDispatchable; use IlluminateQueueInteractsWithQueue; use IlluminateQueueSerializesModels; class SendEmailJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public function __construct() {} public function handle() { // Logic to send an email } }</span>

In this class, we can define the logic for sending the email. Since queue tasks are executed asynchronously, the class must implement the ShouldQueue interface. Additionally, the traits Dispatchable, InteractsWithQueue, Queueable, and SerializesModels provide support for queue operations and object serialization.

Dispatching a Task to the Queue

In Laravel, tasks are dispatched to the queue by calling the dispatch function. Below is an example of dispatching the SendEmailJob task to the queue:

<span class="fun">use AppJobsSendEmailJob; dispatch(new SendEmailJob());</span>

This code dispatches the SendEmailJob task to the default queue. To specify a different queue name, you can use the onQueue method:

<span class="fun">use AppJobsSendEmailJob; dispatch((new SendEmailJob())->onQueue('emails'));</span>

If you want to delay the execution of the task, you can use the delay method:

<span class="fun">use AppJobsSendEmailJob; $job = (new SendEmailJob())->delay(Carbon::now()->addMinutes(10)); dispatch($job);</span>

Executing Queue Tasks

In the Laravel framework, queue tasks can be executed in two ways: synchronously and asynchronously.

Synchronous Execution (Sync Driver)

Synchronous execution means that tasks are executed immediately in the current request, without going through the queue driver. To configure this, set the default queue driver to 'sync' in the config/queue.php file:

<span class="fun">'default' => env('QUEUE_DRIVER', 'sync'),</span>

When this configuration is used, tasks will execute immediately in the current request after being dispatched.

Asynchronous Execution

Asynchronous execution means that tasks are handled by the queue driver. To enable this, set the default queue driver to a different driver, such as 'database', in the config/queue.php file:

<span class="fun">'default' => env('QUEUE_DRIVER', 'database'),</span>

Next, use the following command to listen for and execute queue tasks:

<span class="fun">php artisan queue:work</span>

When this configuration is used, tasks will be added to the queue and executed by the queue:work command.

Conclusion

Through the steps outlined above, we can easily implement the queue functionality in the Laravel framework. The provided example uses sending emails as a queue task. Queue functionality helps handle background tasks more efficiently, improving the concurrency and response speed of the system. In real-world development, you can combine it with other features and third-party services to create even more powerful functionalities. We hope this article helps you better understand and utilize the queue functionality in Laravel.