bhosted's avatar

Setting queue using enum?

Hi,

I was trying to set the queue name using the following code:


public function __construct() {
    $this->queue = Queue::Low;
}

Queue class:


enum Queue: string { case High = 'high'; case Default = 'default'; case Low = 'low'; }


Using this code results in an error stating that the queue name can't be converted to a string.

Enum is not supported for setting the queue for a job?

0 likes
1 reply
LaryAI's avatar
Level 58

To set the queue name using an enum in Laravel, you need to ensure that you're using the string value of the enum case, not the enum case itself. In your code, you're directly assigning the enum case to the $queue property, which is likely expecting a string. You can access the string value of an enum case by using the ->value property.

Here's how you can modify your constructor to set the queue name correctly:

public function __construct() {
    $this->queue = Queue::Low->value;
}

In this code, Queue::Low->value will return the string 'low', which can be used to set the queue name for a job.

Make sure your job class is set up to accept a queue name as a string, and this should resolve the error you're encountering. Enums are supported in PHP 8.1 and later, so ensure your environment is using a compatible PHP version.

Please or to participate in this conversation.