宣布已发布新活动
请使用下面的代码,如果我使用event()可以正常工作,但是如果我添加toOthers
则会给我一个错误提示,为什么?
Route::post('/messages', function(){
$user = Auth::user();
$message = $user->messages()->create([
'message' => request()->get('message')
]);
//Announce that new event have been posted
broadcast(new MessagePosted($message, $user))->toOthers;
return ['status' => 'OK'];
})->middleware('auth');
答案 0 :(得分:1)
toOthers()
是一个函数而非属性
broadcast(new MessagePosted($message, $user))->toOthers();
来自the docs
但是,broadcast
函数还公开了toOthers
方法,该方法使您可以从广播的收件人中排除当前用户:
broadcast(new ShippingStatusUpdated($update))->toOthers();
从vendor/laravel/framework/src/Illuminate/Broadcasting/PendingBroadcast.php
第41行的the source起
public function toOthers()
{
if (method_exists($this->event, 'dontBroadcastToCurrentUser')) {
$this->event->dontBroadcastToCurrentUser();
}
return $this;
}
希望这会有所帮助
答案 1 :(得分:0)
将此添加到Others();
广播(新MessagePosted($ message,$ user))-> toOthers();
答案 2 :(得分:0)
Route::post('/messages', function(){
$user = Auth::user();
$message = $user->messages()->create([
'message' => request()->get('message')
]);
broadcast(new MessagePosted($message, $user))->toOthers();
return ['status' => 'OK'];
})->middleware('auth');