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...