【发布时间】:2019-09-27 04:42:07
【问题描述】:
在 BroadcastServiceProvider.php 中,当用户加入频道时,我有数据,我想将其存储到数据库中。我想知道如何覆盖这个 storeUser() 函数以使其工作(我以前使用过这个函数,但它是在其他情况下使用的)。
public function storeUser() {
UserInfo::create([
'ip' => Request::ip(),
'name' => Auth::user()->name
]);
}
BroadcastServiceProvider.php
Broadcast::channel('chat', function ($user) {
$ip = Request::ip();
if (auth()->check()) {
return [
'id' => $user->id,
'ip' => $ip,
'name' => $user->name
];
}
});
【问题讨论】:
-
storeUser函数在哪里定义的?
-
@jfadich 我已经为它创建了 UsersController.php。
-
为什么不把它移到
UserInfo模型上的静态方法,然后在UsersController和广播类中调用UserInfo:: storeUser()。 -
@jfadich 你能更具体一点吗?我不确定我明白你的意思。