How to call dispatch job inside dispatch job in Laravel 8

Viewed 14

I have a scenario where I want to call a dispatch job inside a job. If the order has been placed in 60 seconds, it will go to the next driver if the driver doesn't respond.

public function index()
{
    $order = Order::create([
        'name' => "order1",
        "driver_id" => "1",
        "status" => "0",
        "price" => "500"

    ]);

    dispatch(new OrderPlacedJob($order))->delay(now()->addSecond(30));
}

Inside my OrderPlacedJob, I am trying to call Orderplacedjob like the following.

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

    protected $order;

    public function __construct(Order $order)
    {
        $this->order = $order;
    }
    
    public function handle()
    {
        $order = $this->order;
        
        if ($this->order->status == '0') {
            $driver_id = $this->order->driver_id + 1;
            $this->order->driver_id = $driver_id;
            $this->order->save();
        }

        OrderPlacedJob::dispatch($order)->delay(now()->addSecond(30));
    }
}

It is working for the first time. If a driver with id one is given the order, but if he doesn't respond within a second, the id changes to 2, then it stops and doesn't run for the second time.

0 Answers
Related