这个问题 我需要广播模型事件给大家除了谁发送请求的一个。(创建最后一条消息的人)。
我希望做一些类似的事情,broadcast(new ChatUpdated)->toOthers
但我需要在活动中进行,因为我不会手动播放它(如下所述)。
该Chat
模型这个样子的
<?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');
}
}
这就是事件
<?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
模型会更新,因此广播事件,
在前面我发送请求
{
headers: {
'X-Socket-ID': window.Echo.socketId(),
}
我如何从活动中告诉谁应该播放?
发布于 2019-06-14 11:08:43
使用带有->toOthers()
该事件选项的状态通道,如下所示:
broadcast(new NewMessage($message))->toOthers();
请参阅此处的文档:https://laravel.com/docs/5.8/broadcasting#presence-channels
https://stackoverflow.com/questions/-100007017
复制相似问题