Laravel5.4 Queue Simple Configuration and Use

  laravel, php, Queue


What is queue?

This is what Baidu Encyclopedia said.

A “queue” is a container that holds data during transmission.

To give a few examples of life:

  • New release of iphone, new product of Sanlitun iphone. Everyone has to queue up to buy, so it can’t be said that a large group of people rushed in together, then the store is finished.
  • You have a lot of girlfriends. You can’t come one by one, can you?
  • The same is true of the concept of heap in C language. First in, first out. You cannot get up early and catch up late.

Get back to business:

Message Queuing is a concept prepared for mass processing of data. It has many implementations and is not a single code structure.

Here is a more professional article: …

There are also tutorials for beginners to ponder:

Nonsense said, completely get to the point:

Laravel provides us with a simple and easily configurable queue class.

To quote a little official translation:

Laravel queues provide a unified API for different background queue services, such as Beanstalk, Amazon SQS, Redis, and even other queues based on relational databases. The purpose of the queue is to delay the processing of time-consuming tasks, such as sending mail, thus greatly shortening the Web request and the corresponding time.

The queue configuration file is stored in config/queue.php Each queue driver configuration can be found in this file, including database, Beanstalkd, Amazon SQS, Redis, and synchronous (locally used) drivers. It also includes a null queue driver for tasks that abandon the queue.

Laravel provided the global function dispatch () directly in version 5.4, and you can call it anywhere. And there is no need to load any objects or instantiate classes. The main purpose of this function is to add your queue requirements to the specified container (the professional name is producer, in fact you can understand it when you finish shopping in the mall and line up for checkout)

Set drive

I won’t say much about Laravel’s magic database migration. I believe you know.

php artisan queue:table
 php artisan migrate

After the above two commands are executed, it is hard to open the configqueue.php. env configuration file loading method is used in the array with key = DEFAULT. After laravel is installed, sync is the default. We need to change to asynchronous (you can temporarily think of synchronization now! = Queue), here we choose to use a relational database to implement the queue.


Create task

Create Task = Create a Producer = (In fact, write a business logic that you want to execute in the queue), name it at will, but it is best to follow the naming convention.

php artisan make:job SendReminderEmail

The generated file is roughly divided into 2 parts: the __construct () construction method and the handle queue execution method (which means the code written in the queue is used when the queue is executed)

class SendReminderEmail implements ShouldQueue
 //you don't have to take a reason to him this one
 use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 protected $name;
 * Create a new job instance.
 * @return void
 public function __construct($name)
 $this->name = $name;
 * Execute the job.
 * @return void
 public function handle()
 'name' => $this->name,


Then use the dispatch method call within the controller, and I’ll create 100 businesses for the loop below.

public function index(Request $request)
 for ($i = 0;   $i <= 100;  $i++) {
 dispatch(new SendReminderEmail("email" . $i));

This is basically the case in the data table that you migrated through the data.


Consumer = queue processing = (you have already started to pay for shopping in the mall). if you use the following command, you will start to consume, and the queue will also start processing according to the business logic above. After processing, the current task will be deleted automatically.

php artisan queue:work

This is basically the following sample


At this point, the simple configuration and use of the Laravel5.4 queue is over.

Please refer to China Laravel for more professional tutorials on aerial bombing. …