Laravel 12 CRUD Operations Using Queues – A Beginner’s Guide

Author

Kritim Yantra

May 08, 2025

Laravel 12 CRUD Operations Using Queues – A Beginner’s Guide

In modern web applications, performance and user experience are crucial. Imagine a scenario where creating a record in your app triggers heavy operations like sending emails, updating third-party APIs, or processing images. Doing these tasks synchronously can slow down your app.

Here’s where Laravel Queues come into play!

In this blog, we’ll learn how to integrate queues with CRUD operations in Laravel to ensure our app runs fast, smooth, and efficiently.


📘 What You’ll Learn

✅ What are Queues in Laravel 12?
✅ Setting up Queues (using Database Driver)
✅ Creating CRUD operations
✅ Dispatching queue jobs during CRUD
✅ Monitoring and running queue workers


🔧 Prerequisites

  • Laravel 12+ installed
  • Basic understanding of Laravel 12 CRUD
  • MySQL or any relational database
  • Composer and PHP installed

🚀 Why Use Queues in CRUD Operations?

When you create, update, or delete a resource, you might want to:

  • Send an email confirmation
  • Log activity
  • Sync data to a third-party service
  • Resize or process uploaded images

Doing these in real-time increases response time. Queues help offload these tasks, improving the response speed and scalability of your app.


📁 Step 1: Create a New Laravel 12 Project

composer create-project laravel/laravel laravel-crud-queue
cd laravel-crud-queue

️ Step 2: Configure Queue Driver

Let’s use the database queue driver for simplicity.

1. Update .env file

QUEUE_CONNECTION=database

2. Create the jobs table

php artisan queue:table
php artisan migrate

Now Laravel is ready to store and handle queued jobs in the database.


📦 Step 3: Create a Model, Migration & Controller

Let’s build a simple Post model.

php artisan make:model Post -mcr

Migration: database/migrations/xxxx_xx_xx_create_posts_table.php

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->text('body');
    $table->timestamps();
});
php artisan migrate

✍️ Step 4: Setup Routes and Controller Methods

routes/web.php

use App\Http\Controllers\PostController;

Route::resource('posts', PostController::class);

app/Http/Controllers/PostController.php

Add basic CRUD logic here. For now, focus on the store method where we’ll dispatch a job.


🛠️ Step 5: Create a Job for Post-Creation Logic

php artisan make:job ProcessPostCreation

app/Jobs/ProcessPostCreation.php

namespace App\Jobs;

use App\Models\Post;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessPostCreation implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    protected $post;

    public function __construct(Post $post)
    {
        $this->post = $post;
    }

    public function handle(): void
    {
        // Simulate sending an email or logging
        logger("Post '{$this->post->title}' created and processed via queue.");
    }
}

📝 Step 6: Update the Store Method to Dispatch Job

PostController.php

use App\Models\Post;
use Illuminate\Http\Request;
use App\Jobs\ProcessPostCreation;

public function store(Request $request)
{
    $data = $request->validate([
        'title' => 'required|string|max:255',
        'body'  => 'required|string',
    ]);

    $post = Post::create($data);

    // Dispatch job
    ProcessPostCreation::dispatch($post);

    return redirect()->route('posts.index')->with('success', 'Post created and job dispatched!');
}

️ Step 7: Run the Queue Worker

In a separate terminal:

php artisan queue:work

This starts the worker that processes queued jobs.


👀 Step 8: Testing the Setup

  1. Start Laravel server:
php artisan serve
  1. Visit /posts/create and create a new post.
  2. Watch your terminal where queue:work is running. You’ll see the log message from the job.

✅ Other CRUD Hooks with Jobs

You can use jobs in other methods too:

  • update() – Dispatch a job to log updates
  • destroy() – Dispatch a job to notify admin of deletion
  • Use Model Observers to hook into model events and automatically dispatch jobs

🧠 Bonus: Use Delayed Jobs

You can delay a job like so:

ProcessPostCreation::dispatch($post)->delay(now()->addMinutes(1));

📊 Monitor Queues (Optional)

You can use Laravel Horizon for Redis-powered queue monitoring, but for beginners, the database driver is easy to start with.


🎉 Conclusion

You just learned how to:

  • Set up Laravel Queues using the database driver
  • Integrate queues with a CRUD app
  • Create and dispatch jobs
  • Improve app performance by deferring time-consuming tasks

Using queues is a best practice in Laravel when building scalable, modern web applications. As your app grows, you’ll be thankful for this separation of concerns and performance boost.


💡 Next Steps

  • Try using queues with image processing (e.g., Intervention Image)
  • Explore Laravel Horizon for better queue management
  • Learn about Job Batching and Chains for advanced use-cases

📬 Stay Connected

If you found this helpful, consider subscribing to our newsletter or following us on YouTube for more Laravel content!

Happy Coding! 

Tags

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts