Milind Daraniya

How to Use Laravel Queues for Background Jobs (with Example)

Published October 2nd, 2025 10 min read

When your Laravel application grows, some tasks take more time like sending emails, generating PDF reports, importing Excel files, or resizing images. If you run these tasks directly inside the controller, your page will become slow.

Laravel Queues help by sending heavy tasks to the background, so your user gets a fast response.

In this blog, I explain queues step-by-step with a proper example using Database Queue.

Step 1 — Set Queue Driver

Open .env file:

 
QUEUE_CONNECTION=database

Laravel will now store jobs inside your database.


Step 2 — Create Queue Jobs Table

Run:

 
php artisan queue:table php artisan migrate

This creates a jobs table that stores all pending background jobs.


Step 3 — Create a Job Class

Example: send a welcome email in the background.

 
php artisan make:job SendWelcomeEmail

Open the job file:

 
namespace App\Jobs;

use App\Models\User
use Illuminate\Bus\Queueable
use Illuminate\Contracts\Queue\ShouldQueue
use Illuminate\Queue\InteractsWithQueue
use Illuminate\Queue\SerializesModels
use Illuminate\Support\Facades\Mail

class SendWelcomeEmail implements ShouldQueue 
{    
use Queueable, InteractsWithQueue, SerializesModels;    
public $user;    
public function __construct(User $user)    {
        $this->user = $user;    
}
    public function handle()
    {
        Mail::raw("Welcome " . $this->user->name, function ($msg) {
            $msg->to($this->user->email)
                ->subject("Welcome to Our App");
        });
    }
 }

This job will send an email in the background.


Step 4 — Dispatch the Job from Controller

Inside your controller:

 
use App\Jobs\SendWelcomeEmail
use App\Models\User
public function register(Request $request) {
    $user = User::create([
        "name" => $request->name,
        "email" => $request->email,
        "password" => bcrypt($request->password),
    ]);    
// Queue job    
SendWelcomeEmail::dispatch($user);
    return response()->json(["message" => "User created. Email will be sent in background."]);
 }

Now the controller is fast because the email task is queued.


Step 5 — Run Queue Worker

To process queued jobs, you must run:

 
php artisan queue:work

This worker keeps checking the jobs table and runs pending tasks.


Step 6 — Test the Example

Register a new user

You will receive JSON response instantly

Email will be sent in background by the queue worker

You can check jobs and failed_jobs tables for debugging.


Step 7 — Running Queues in Production (Important)

On live server, queue worker must run 24/7.
Use Supervisor:

 
[program:laravel-worker] process_name=%(program_name)s_%(process_num)02d command=php /var/www/html/artisan queue:work --sleep=3 --tries=3 numprocs=1 autostart=true autorestart=true redirect_stderr=true stdout_logfile=/var/log/laravel-worker.log

Supervisor ensures your queue always stays active even after server restart.


When to Use Queues

You should use queues for:

Sending emails

Creating invoices

Generating large PDF reports

Resizing/uploading images

Importing/Exporting Excel files

Sending notifications

Third-party API calls

SMS sending

Video processing

Anything slow → send to queue.