Laravel queues emails via notifications: Add ShouldQueue to notification class. Dispatch with $user->notify(), run queue:work. No job files—async sends, delays, retries. Simplify code, boost app speed. #Laravel
Queues in Laravel are a game-changer for improving application performance, especially when sending emails, generating reports, or handling resource-intensive tasks. But what if you want to queue notifications (like emails) without creating a separate job file? Laravel’s built-in notification system and queue integration make this effortless! Here’s how to do it.
Why Use Queues for Notifications?
- No Extra Job Classes Needed: Laravel notifications can be queued natively.
- Simpler Code: Handle email logic directly in notifications while still leveraging queues.
- Automatic Retries: Failed notifications are tracked and can be retried later.
- Better User Experience: Send emails asynchronously without delaying HTTP responses.
Step 1: Configure the Queue Driver
First, set up your queue driver in .env. We’ll use the database driver for simplicity:
QUEUE_CONNECTION=databaseCreate the required tables for queues (note: in laravel 12 by defaul already have required queues tables):
php artisan queue:table
php artisan migrateStep 2: Create a Queued Notification
Generate a notification class. This will handle email logic and queuing:
php artisan make:notification WelcomeEmailNotificationOpen app/Notifications/WelcomeEmailNotification.php and modify it to implement queuing:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; // Add this
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
// Implement ShouldQueue
class WelcomeEmailNotification extends Notification implements ShouldQueue
{
use Queueable; // Enable queuing
public function __construct()
{
// Add data if needed (e.g., user details)
}
public function via($notifiable)
{
return ['mail']; // Send via email
}
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Welcome to Our Application!')
->line('Thank you for signing up!')
->action('Visit Dashboard', url('/dashboard'));
}
}By adding implements ShouldQueue, Laravel will automatically queue the notification.
Step 3: Dispatch the Notification
Trigger the notification from your controller or service. For example, when a user registers:
use App\Notifications\WelcomeEmailNotification;
public function register(User $user)
{
// Queue the notification
$user->notify(new WelcomeEmailNotification());
return redirect('/home')->with('success', 'Welcome! Check your email.');
}The notify() method dispatches the notification to the queue automatically.
Step 4: Run the Queue Worker
Start processing queued notifications with:
php artisan queue:workThis command listens for jobs (including notifications) and executes them.
For production, use Supervisor to keep the worker running.
Why This Works
Under the hood, Laravel wraps the notification in a SendQueuedNotifications job class automatically.
You don’t need to create a job file manually—just implement ShouldQueue in your notification, and Laravel handles the rest!
Conclusion
Queuing notifications in Laravel is incredibly straightforward. By leveraging the ShouldQueue interface, you can defer time-consuming tasks like sending emails without writing extra boilerplate code for jobs. This keeps your codebase clean while ensuring your application remains responsive.
Next Steps:
- Explore Laravel Notification Docs for more features (e.g., SMS, Slack).
- Use Laravel Horizon for advanced queue monitoring.
- Configure Supervisor for robust queue management in production.
Now go queue those notifications and turbocharge your app’s performance! 🚀
Happy Coding! 😊