Laravel でキューを使うには?

ショコラ
ショコラ

Laravel でキューを使うには?

もっさん先輩
もっさん先輩

設定ファイル .env の修正

QUEUE_CONNECTION=database
QUEUE_DRIVER=database

DBの準備

./artisan queue:table
./artisan migrate

JoBファイルの作成。app/Jobs/TestJob.php にファイルができます。

./artisan make:job TestJob

<?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 TestJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

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

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

コントローラー側で dispatch でキューに入れる。

TestJob::dispatch();

ジョブを実行するのはこちら

./artisan queue:work

以上

Scroll to Top