简体   繁体   中英

Second parameter not being passed to Event in Laravel 5.1

I'm trying to construct an event with two parameters, a boolean and then a possibly null second argument. The interesting thing is, I already have one event working with exactly the same setup. But I can't get the second event to function correctly.

Here's the functioning event:

namespace MyProj\Events\Live;

use MyProj\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class LiveCountdownEvent extends Event  implements ShouldBroadcast
{
    use SerializesModels;

    public $isResuming;
    public $newTime;

    /**
     * Create a new event instance.
     *
     * @param $isResuming
     * @param null $newTime
     */
    public function __construct($isResuming, $newTime= null)
    {
        $this->isResuming = $isResuming;
        if ($newTime!= null) {
            $this->newTime= $newTime->format('Y-m-d H:i:s');
        }

    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return ['live-updates'];
    }
}

Which, when it comes through on my front end via websockets:

Object { isResuming: false, newTime: null }

Here's the non-functional event, with practically no changes.

<?php

namespace MyProj\Events;

use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class WebcastEvent extends Event  implements ShouldBroadcast
{
    use SerializesModels;

    public $isActive;
    public $videoId;

    /**
     * Create a new event instance.
     *
     * @param $isActive
     * @param null $videoId
     */
    public function __construct($isActive, $videoId = null)
    {
        $this->isActive = $isActive;
        if ($videoId != null) {
            $this->videoId = $videoId;
        }

    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return ['live-updates'];
    }
}

This one doesn't return the videoId , even if it's set when I pass it through (using Logging statements, I confirm the event is constructed correctly):

Object { isActive: true }

Notice the lack of a videoId ? What's going on here?

I have created the event Classes and I fired them, and works properly.

Event::fire(new LiveCountdownEvent(true));
Event::fire(new WebcastEvent(true));

Log:

[2015-12-03 12:24:21] local.INFO: Broadcasting [App\Events\LiveCountdownEvent] on channels [live-updates] with payload:
{
    "isResuming": true,
    "newTime": null
}  
[2015-12-03 12:24:21] local.INFO: Broadcasting [App\Events\WebcastEvent] on channels [live-updates] with payload:
{
    "isActive": true,
    "videoId": null
}

I know that my answer does not solve your problem but it maybe help you to face the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM