在 BroadcastServiceProvider.php 中,当用户加入频道时我有数据,我想将其存储到数据库中。我想知道如何覆盖这个 storeUser() 函数以使其工作(我以前使用过这个函数,但在其他情况下使用过)。
public function storeUser() {
UserInfo::create([
'ip' => Request::ip(),
'name' => Auth::user()->name
]);
}
广播服务提供者.php
Broadcast::channel('chat', function ($user) {
$ip = Request::ip();
if (auth()->check()) {
return [
'id' => $user->id,
'ip' => $ip,
'name' => $user->name
];
}
});
更新UserInfo
模型以具有该storeUser
方法。
class UserInfo
{
public static function storeUser() {
UserInfo::create([
'ip' => Request::ip(),
'name' => Auth::user()->name
]);
}
然后你可以在广播器中调用它
Broadcast::channel('chat', function ($user) {
$ip = Request::ip();
if (auth()->check()) {
UserInfo::storeUser();
return [
'id' => $user->id,
'ip' => $ip,
'name' => $user->name
];
}
});
您也可以在UserInfo::storeUser();
需要时在用户控制器中以相同的方式调用它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句