Courses

Queues in Laravel 12

Regular Jobs: Creating, Dispatching and Adding Parameters

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Creating dedicated Job classes with php artisan make:job (includes ShouldQueue by default)
- Dispatching jobs using JobName::dispatch() method
- Passing parameters to jobs through constructor and using them in the handle() method
- Serializing models in job payloads for use during processing

In the first two lessons of the course, we examined examples of classes that can easily implement ShouldQueue.

But generally speaking, and as I mentioned previously, there are three things for queues: jobs, queues, and workers. So, a job is a global thing that you can generate and put into a queue. Let's try to generate a simple job and put it into a queue.


Creating a Laravel Job

First, let's create a job using the Artisan command:

php artisan make:job SendRegisteredUserNotification

The main difference from classes we covered in the previous lessons is that jobs are generated by default with the Queueable trait added and the ShouldQueue interface implemented.

app/Jobs/SendRegisteredUserNotification.php:

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
 
class SendRegisteredUserNotification implements ShouldQueue
{
use Queueable;
 
public function __construct()
{
//
}
 
public function handle(): void
{
//
}
}

All of the job logic is applied in the handle() method. Now, we can move the mail sending logic from...

The full lesson is only for Premium Members.
Want to access all 15 lessons of this course? (46 min read)

You also get:

  • 76 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord