lundi 12 novembre 2018

Laravel Broadcast event - how to define private channel name dynamically

I'm learning how to create broadcasted event. I want to notify customer after order status changed. I created Order_Modified event. My problem is that notification is not displayed on private channel. On public channel everything works fine.

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  modified created by 
</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;
});

I think the problem is e.order.id. I don't have any idea how to pass order id to Echo.private method. Please help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire