Laravel Broadcast事件-如何动态定义私有频道名称

时间:2018-11-11 20:52:23

标签: laravel-5 laravel-events laravel-broadcast

我正在学习如何创建广播事件。我想在订单状态更改后通知客户。我创建了 Order_Modified 事件。我的问题是通知未显示在私人频道上。在公共频道上,一切正常。

class Order_Modified implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
 * Create a new event instance.
 *
 * @return void
 */

public $order;
public $user;

public function __construct($order, $user)
{
    $this->order = $order;
    $this->user = $user;
}

/**
 * Get the channels the event should broadcast on.
 *
 * @return \Illuminate\Broadcasting\Channel|array
 */
public function broadcastOn()
{
    return new PrivateChannel('order.' . $this->order->id);
}
}

/// notification.vue

<template>
<div>

 <div v-if="mes" class="notification is-primary">
  <button v-on:click=za() class="delete"></button> Order {{nr}} modified created by {{us}}
</div>

</div>
</template>

<script>
export default {
data: function() {
return {
  mes: false,
  nr: 0,
  us: 0
 };
 },
 mounted() {
 Echo.private("order." + e.order.id).listen(
  "Order_Modified",
  e => {
    this.mes = true;
    this.nr = e.order.id;
    this.us = e.user.id;
   }
 );
},
methods: {
  za: function() {
  this.mes = false;
 }
}
} ;

/// channels.php

Broadcast::channel('order.{id}', function ($user, $id) {
return $user->id === \App\Order::find($id)->users_id;
});

我认为问题是e.order.id。我不知道如何将订单ID传递给Echo.private方法。请帮忙。

0 个答案:

没有答案