如何在事件Laravel中获取有效载荷?

时间:2019-09-25 04:05:24

标签: laravel-5

在我的控制器中,我有此代码。

event(new ExampleEvent(), ['data' => '123']);

事件帮助程序具有有效负载参数。我正在尝试研究是否使用有效载荷获取数据值。在我的听众中

class ExampleListener implements ShouldQueue
{
    use InteractsWithQueue;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  ExampleEvent  $event
     * @return void
     */
    public function handle(ExampleEvent $event)
    {
         // dd($this->payload);
    }
}

我想获取有效载荷中的数据值,就像我将其添加到控制器中一样。我正在google中搜索有关如何获取有效载荷的信息,但未能学习。

1 个答案:

答案 0 :(得分:0)

如果将对象作为第一个参数传递给event()函数, 其他所有参数都将被忽略。

您可以将有效负载传递给事件类:

event(new ExampleEvent('123')); // handle the payload in the constructor

监听器:

public function handle(ExampleEvent $event)
{
     $event->data; // 123
}

OR

您可以使用字符串作为事件名称:

event('ExampleEvent', ['data' => '123']);

监听器:

public function handle($payload) // no type hint!
{
     //$payload == '123'
}