首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >广播模型仅向其他人更新事件

广播模型仅向其他人更新事件
EN

Stack Overflow用户
提问于 2019-06-14 01:12:49
回答 1查看 0关注 0票数 0

这个问题 我需要广播模型事件给大家除了谁发送请求的一个。(创建最后一条消息的人)。

我希望做一些类似的事情,broadcast(new ChatUpdated)->toOthers但我需要在活动中进行,因为我不会手动播放它(如下所述)。

Chat模型这个样子的

代码语言:javascript
复制
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Traits\RelationshipsTrait;
use App\Traits\Updatable;
use Illuminate\Database\Eloquent\SoftDeletes;

class Chat extends Model
{
  use SoftDeletes, RelationshipsTrait, Updatable;

  protected $dispatchesEvents = [
    'updated' => 'App\Events\ChatUpdated',
  ];

  public function messages()
  {
    return $this->hasMany('App\Models\Message')->orderBy('created_at');
  }
}

这就是事件

代码语言:javascript
复制
<?php

namespace App\Events;

use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use App\Http\Resources\ChatResource;
use App\Models\Chat;

class ChatUpdated implements ShouldBroadcastNow
{
  use Dispatchable, InteractsWithSockets, SerializesModels;

  public $chat;

  public function __construct(Chat $chat)
  {
    $this->chat = new ChatResource($chat));
  }

  public function broadcastOn()
  {
    return [
      new PrivateChannel($this->chat->channel.'.chatCenter'),
    ];
  }

  public function broadcastAs()
  {
    return 'ChatUpdated';
  }
}

updated event是beign自动播放每一个模型是时间updated

当用户发送新消息时,Chat模型会更新,因此广播事件,

在前面我发送请求

代码语言:javascript
复制
{
    headers: {
        'X-Socket-ID': window.Echo.socketId(),
}

我如何从活动中告诉谁应该播放?

EN

回答 1

Stack Overflow用户

发布于 2019-06-14 11:08:43

使用带有->toOthers()该事件选项的状态通道,如下所示:

代码语言:javascript
复制
broadcast(new NewMessage($message))->toOthers();

请参阅此处的文档:https//laravel.com/docs/5.8/broadcasting#presence-channels

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100007017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档