Laravel job middleware WithoutOverlapping - must not be accessed before initialization (php)

Viewed 723

I am using Laravel 8 with PHP 7.4 and trying to implement a job that has the middleware of "WithoutOverlapping".

The job is dispatched onto a queue with the argument of "1", when the job is executed on the queue I receive the following error:

$hotelID must not be accessed before initialization

It seems that the __construct method is not being called before the middleware, how would I pass a key to the class constructor of WithoutOverlapping?

class DeleteHotel implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, HotelContentCache;

    private int $hotelID;

    public function __construct(int $hotelID)
    {
        $this->hotelID = $hotelID;
    }

    public function middleware(): array
    {
        return [(new WithoutOverlapping($this->hotelID))->releaseAfter(10)];
    }
}
1 Answers

You have to change

private int $hotelID;

to

protected int $hotelID;

and it will work.

Related