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
- Start Laravel server:
php artisan serve
- Visit
/posts/createand create a new post. - Watch your terminal where
queue:workis 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 updatesdestroy()– 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!
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.