Kritim Yantra
May 08, 2025
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 are Queues in Laravel 12?
✅ Setting up Queues (using Database Driver)
✅ Creating CRUD operations
✅ Dispatching queue jobs during CRUD
✅ Monitoring and running queue workers
When you create, update, or delete a resource, you might want to:
Doing these in real-time increases response time. Queues help offload these tasks, improving the response speed and scalability of your app.
composer create-project laravel/laravel laravel-crud-queue
cd laravel-crud-queue
Let’s use the database
queue driver for simplicity.
.env
fileQUEUE_CONNECTION=database
php artisan queue:table
php artisan migrate
Now Laravel is ready to store and handle queued jobs in the database.
Let’s build a simple Post model.
php artisan make:model Post -mcr
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
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.
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.");
}
}
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!');
}
In a separate terminal:
php artisan queue:work
This starts the worker that processes queued jobs.
php artisan serve
/posts/create
and create a new post.queue:work
is running. You’ll see the log message from the job.You can use jobs in other methods too:
update()
– Dispatch a job to log updatesdestroy()
– Dispatch a job to notify admin of deletionYou can delay a job like so:
ProcessPostCreation::dispatch($post)->delay(now()->addMinutes(1));
You can use Laravel Horizon for Redis-powered queue monitoring, but for beginners, the database driver is easy to start with.
You just learned how to:
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.
If you found this helpful, consider subscribing to our newsletter or following us on YouTube for more Laravel content!
Happy Coding!
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google