因此,我将Laravel Event与Pusher一起使用,到目前为止,如果我在没有任何额外条件的情况下使用公共频道,则可以在刀片中获取数据作为通知。
当我尝试仅向1个用户而非所有人发送通知时,问题就开始了,我已经尝试过PrivateChannel
,但我得到了:
[Vue warn]: Error in created hook: "ReferenceError: bidId is not defined"
and
ReferenceError: bidId is not defined
仅向项目所有者发送通知(他/她的ID
保存在项目表中)
<template>
<li class="nav-item dropdown">
<a id="navbarDropdown" class="nav-link icon-menu dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
<span class="sr-only">Notifications</span>
<i class="far fa-bell"></i>
<span class="notification-dot"></span>
</a>
<div class="dropdown-menu notifications dropdown-menu-right" aria-labelledby="navbarDropdown">
<a class="dropdown-header"><strong>You have {{bids.length}} new notifications</strong></a>
<a class="dropdown-item" v-for="bid in bids">
<div class="media">
<div class="media-left">
<i class="fa fa-fw fa-flag-checkered text-muted"></i>
</div>
<div class="media-body">
<p class="text">{{bid.message}}</p>
<span class="timestamp">{{bid.created_at}}</span>
</div>
</div>
</a>
<a class="dropdown-item more">See all notifications</a>
</div>
</li>
</template>
<script>
export default {
data() {
return{
bids:[],
}
},
created(){
this.fetchBids();
},
methods:{
fetchBids(){
// Echo.channel('bidplaced') //tested with public channel
Echo.private(`bidplaced.${bidId}`)
.listen('BidPlaced', (e) => {
this.bids.push(e.bid);
});
},
},
}
</script>
<notifications></notifications>
$bid->save();
event(new BidPlaced($bid)); //firing the event
// this is 1 way to get the user i need to receive notification
$user = $bid->project->user;
class BidPlaced implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $bid;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Bid $bid)
{
$this->bid = $bid;
}
public function broadcastOn()
{
// return ['bidplaced']; //this is working as public channel
return new PrivateChannel('bidplaced.'.$this->bid->project->user_id);
}
如何让我的项目所有者作为这些通知的接收者?
答案 0 :(得分:0)
可能会晚一点,但希望这对某人有帮助!
控制器
broadcast(new BidPlaced($user))->toOthers();
事件
use App\User
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function broadcastOn()
{
return new PrivateChannel('notification.'.$this->user->id);
}
Vue组件
created() {
Echo.private('notification.'+auth.user.id)
.listen('BidPlaced', (e) => {
console.log(e);
});
},