Courses

Queues in Laravel 12

Jobs into Groups: Batching and Chaining

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Bus::chain() runs jobs sequentially, with later jobs depending on successful completion of previous ones
- Bus::batch() runs jobs in parallel, providing better monitoring capabilities
- Adding the Batchable trait to job classes to use them in batches
- Using batch lifecycle hooks (before, progress, then, catch, finally) for monitoring and handling exceptions
- Implementing batch cancellation checks within jobs with $this->batch()->canceled()

In this lesson, we'll explore how to organize and group multiple Laravel jobs using two powerful techniques: chaining and batching. These approaches help you execute related jobs in a controlled manner with better monitoring.


The Scenario

Imagine a user registers, and you want to perform several jobs afterward. For example, create the first task, then send a welcome message via the internal messaging system and notify the admin about the new users. What if you want to group all of those and monitor whether they succeed or not?

app/Http/Controllers/Auth/RegisteredUserController.php:

use App\Jobs\CreateFirstTask;
use App\Jobs\SendWelcomeMessage;
use App\Jobs\SendRegisteredUserNotification;
 
class RegisteredUserController extends Controller
{
// ...
 
public function store(Request $request): RedirectResponse
{
// ...
 
CreateFirstTask::dispatch($user->id);
SendWelcomeMessage::dispatch($user->id);
SendRegisteredUserNotification::dispatch($user);
 
Auth::login($user);
 
return redirect(route('dashboard', absolute: false));
}
}

Options For Grouping Laravel Jobs

In Laravel, there are two ways to group jobs: batching and chaining. What is the difference?

Imagine a scenario where you're firing...

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