【发布时间】:2017-10-19 23:51:05
【问题描述】:
如何在 Laravel 5.5 中将一些变量传递给事件? 我尝试了几种模式但没有工作。这是我的代码。有人有什么建议吗? 基本上我需要通过套接字更新关注者的数量。使用 Redis 和 Socket.io 的服务器,也可以正常工作
Route::post('follow', function() {
$negozio = Input::get('id_azienda');
$followers = new \App\Models\Followers;
$followers->entry_by = \Session::get('uid');
$followers->id_azienda = $negozio;
$followers->save();
$this->variabili['negozio'] = $negozio;
$this->variabili['followers'] = $followers->count();
event(new \App\Events\Follow(), $this->variabili);
});
这就是事件
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class Follow implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public $variabili;
public function __construct()
{
$this->data = array(
'count'=> $variabili['followers'],
'negozio'=> $variabili['negozio']
);
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return ['test-channel'];
}
}
【问题讨论】: