Laravel queue event not serializing models

2019-06-04 11:40发布

Not sure if i did something wrong, but when attempting do broadcast an event, the SerializesModels only serializes the first model I throw at it.

class WorkerQueueEvent implements ShouldBroadcast
{
  use Dispatchable, InteractsWithSockets, SerializesModels;

  protected $user;
  public $queue;
  public function __construct(User $user,QueueJob $queue)
  {
    $this->user = $user;
    $this->queue = $queue;
  }
  public function broadcastOn()
  {    
    return new PrivateChannel('Queues/' . $this->user->id);
  }
}

This results in the queue never being processed. If i do an dd($queue); or dd($user); in the constructer, it does log both of them, but it never reaches broadcastOn();

I did got it to work, passing QueueJob id and fetching it on the broadcast function, just asking if i was doing something wrong or if this is in fact an error.

2条回答
女痞
2楼-- · 2019-06-04 12:14

I believe the issue here is your public $queue property.

The documentation states that you can specify an event queue by defining the $broadcastQueue property. However, it doesn't mention that it also checks for the $queue property if $broadcastQueue is not defined.

Since you have defined a $queue property, it will attempt to push your event onto the queue named by the __toString() value of your QueueJob model instance, which I'm assuming doesn't exist and why your job is never sent to a queue.

If you rename your property to something like $queueJob, then I believe it should work for you.

查看更多
叛逆
3楼-- · 2019-06-04 12:16

for running queues you have to run php artisan queue:work

for this also make sure that driver is database you can set it your .env file.

Hope this helps

查看更多
登录 后发表回答