How to use Queues in Laravel 9?

May 12, 2022 . Admin

Hi Guys,

In this tutorial,I will learn you how to use queue in laravel 9.Laravel 9 Queues allow you to delay a time-consuming task until a later time. By delaying the time-consuming task, you can improve the performance of the Laravel application significantly.

In this post, we will discuss Laravel Queues which is one of the best features of the Laravel framework.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Database Setup for Laravel Queues

Before using the Laravel Queues, we need to make a jobs table in the database to store all queues. Laravel provides the table creation command out of the box, so go to your terminal and run the below command.

php artisan queue:table

This command will create a migration file in the database/migrations folder. The newly created file will contain the schema for the jobs table which we need to process the queues.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateJobsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('jobs', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('queue')->index();
            $table->longText('payload');
            $table->unsignedTinyInteger('attempts');
            $table->unsignedInteger('reserved_at')->nullable();
            $table->unsignedInteger('available_at');
            $table->unsignedInteger('created_at');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('jobs');
    }
}

Now it’s time to run the migrate command which will create the jobs table in database. Run below command to migrate.

php artisan migrate
Step 3: Create Jobs

Next, we need to create a Laravel Job which will just return a simple view with a welcome message. Run the following command to create a job.

php artisan make:job ProductLaunch

Once the above command finishes, a new folder with name job along with a productlaunch class file will be generated in the app folder.

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProductLaunch implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
    }
}

It will help you...

#Laravel 9