Kritim Yantra
Apr 29, 2025
Laravel Queues are like having a team of assistants who handle tedious work behind the scenes while your app stays fast and responsive. Whether it’s sending emails, processing images, or generating reports, queues ensure your users never wait. Let’s break down how to use Laravel 12 queues in simple terms, with practical examples anyone can follow!
Imagine a pizza delivery app:
Queues help you:
✅ Speed up response times
✅ Handle failures gracefully (retry failed tasks)
✅ Prioritize important jobs (e.g., process payments before sending emails)
Laravel supports Redis, Amazon SQS, Beanstalkd, and databases. For simplicity, let’s use the database driver.
.env
:QUEUE_CONNECTION=database
php artisan queue:table
php artisan migrate
Jobs are tasks you want to run in the background. Let’s create a job to send a welcome email.
php artisan make:job SendWelcomeEmail
This creates app/Jobs/SendWelcomeEmail.php
.
// app/Jobs/SendWelcomeEmail.php
public function handle()
{
$user = $this->user; // Passed via constructor
Mail::to($user->email)->send(new WelcomeEmail($user));
}
Send the job to the queue from anywhere in your code:
use App\Jobs\SendWelcomeEmail;
public function register(User $user)
{
// Dispatch the job
SendWelcomeEmail::dispatch($user);
return "Check your email shortly!";
}
// Send email after 10 minutes
SendWelcomeEmail::dispatch($user)
->delay(now()->addMinutes(10));
Workers process jobs in the queue. Start a worker:
php artisan queue:work
Pro Tips:
--queue=high,default
to prioritize jobs (e.g., high
first). --tries=3
to retry failed jobs 3 times.// app/Jobs/ProcessImage.php
public function handle()
{
$image = Image::load($this->filePath);
$image->resize(800, 600)->save();
}
Dispatch:
ProcessImage::dispatch($filePath);
// app/Jobs/GenerateReport.php
public function handle()
{
$report = PDF::generateReport($this->user);
Storage::put("reports/{$this->user->id}.pdf", $report);
Mail::to($this->user->email)->send(new ReportReady($report));
}
Dispatch with Priority:
GenerateReport::dispatch($user)->onQueue('reports');
Then run workers for the reports
queue:
php artisan queue:work --queue=reports
Sometimes jobs fail (e.g., API timeouts). Laravel makes retries easy.
.env
:QUEUE_TRIES=3
php artisan queue:retry all
// In your job class
public function failed(Exception $e)
{
Log::error("Job failed: " . $e->getMessage());
}
Run jobs automatically at specific times using the schedule
method in app/Console/Kernel.php
:
// Send weekly newsletters every Monday
protected function schedule(Schedule $schedule)
{
$schedule->job(new SendNewsletter)->weekly()->mondays();
}
Run the scheduler:
php artisan schedule:work
composer require laravel/horizon
). sync
driver – Set QUEUE_CONNECTION=sync
in .env
during testing.Laravel Queues turn your app into a well-oiled machine:
Start with simple jobs like sending emails, then explore advanced features like prioritized queues and scheduling. Before you know it, you’ll wonder how you ever built apps without queues!
Next Steps:
Now go offload those tasks and let Laravel Queues do the heavy lifting! 💪
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google