1、在 app\Providers\EventServiceProvider
中注册监听器:
/** * The event listener mappings for the application. * * @var array */ protected $listen = [ ...... 'App\Events\Statistics' => [ 'App\Listeners\BehavioralStatistics', ], ...... ];
2、执行
php artisan event:generate
生成事件类与监听类
3、定义事件
user = $user;
$this->obj = $obj;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}4、定义监听器:
obj);
$statics_view = new StaticsView;
switch($obj_class){
case "App\\User":
$statics_view->statics_type = 'user';
break;
case "App\\Production":
$statics_view->statics_type = 'production';
break;
}
$statics_view->ip = request()->getClientIp();;
$statics_view->time_local = 0;
$statics_view->statics_id = $event->obj->id;
$statics_view->save();
}
}5、触发事件:
event(new Statistics(user, user,user,production));