Despite the documentation declaring otherwise, attempting to set the connection name within a Job class can fail in Laravel with the error:

[Job Class] and Illuminate\Bus\Queueable define the same property ($connection) in the composition of [Job Class]. However, the definition differs and is considered incompatible. Class was composed

1 Answers

1
JackalopeZero On

I believe this is a compatibility issue between PHP 7.3 and Laravel 5.8. The error is raised because the Queueable trait has already defined the 'connection' class variable.

To fix the error, we just need to set the variable rather than declare it.

Broken job class:

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

    public $connection = 'database';
    protected $product;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Product $product)
        $this->product = $product;
    }...

Fixed job class:

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

    protected $product;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Product $product)
    {
        $this->connection = 'database';
        $this->product = $product;
    }...