Queue(佇列) 的功能是讓需要長時間、或可以在背景執行的任務拉出來另外處理,不用在當下等待執行完成

1.首先建立Jobs

1630141486812.jpg

php artisan make:job UpdateProductPrice

2.執行完後目錄就會多一個Jobs

1630141581897.jpg

3.UpdateProductPrice這個主要就是協助我們幫助Product作價格的改變

首先先建立傳入產品並建立我們的attribute

protected $product;
    public function __construct($product)
    {
        $this->product = $product;
    }

4.主程式的函式為handle()

設定一個sleep(5)函式->指的是程式暫停5秒鐘

設定產品價格更新隨機增加2或5的整數金額

設定完成後這些Jobs可以存在1.資料庫裡 2.Redis裡

而Redis是一種存放在本機方式的輕量化資料庫

public function handle()
    {
        sleep(5);
        $this->product->update(['price' => $this->product->price * random_int(2,5)]);
    }

5.這邊試著用資料庫的方式存儲