Courses

How to Structure Laravel 12 Projects

Dispatch Jobs into Background Queue

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Creating Jobs for background processing
- Using queues for async operations
- Implementing job dispatch in Controllers
- Managing job dependencies and data

Imagine a sequence of actions to be performed "in the background", in addition to creating the main logic.

For example, after user registration, you want to prepare demo data for their dashboard. You may want to put it into the background queue so the user won't wait for the operation to finish.

This is where Job classes come to help you.

Jobs are similar to Actions we discussed earlier, but Jobs may be put in a Queue. And they have Artisan command to create them:

php artisan make:job NewUserDataJob

And our job would look like this:

app/Jobs/NewUserDataJob.php:

class NewUserDataJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
public function __construct(public User $user)
{}
 
public function handle()
{
Project::create([
'user_id' => $this->user->id,
'name' => 'Demo project 1',
]);
 
Category::create([
'user_id' => $this->user->id,
'name' => 'Demo category 1',
]);
 
Category::create([
'user_id' => $this->user->id,
'name' => 'Demo category 2',
]);
}
}

All that's left is to...

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

You also get:

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