1

我在我的项目中使用 Laravel Echo 向客户端广播事件。一切正常,但是,我在eager-loading事件模型的关系中遇到了问题。

我的控制器

$chat = new Chat;
$chat->user_id = $request->user_id;
$chat->message = $request->message;
$chat->save();

event( new GetMessages( $chat ));

我的活动课程:

class GetMessages implements ShouldBroadcast

{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $chats;
    public function __construct($chats)
    {
        $this->chats = $chats;
    }

    public function broadcastOn()
    {
        return ['get-messages'];
    }

    public function broadcastAs()
    {
        return 'get-messages';
    }
}

这是我的 Laravel Echo

Echo.channel('get-messages')
    .listen('.get-messages', (data) => {
        console.log(data)
    })

除了attachments Chat.

console.log(data.chats); // Undefined

有什么我错过的吗?因为当我eager-loaded从 Event 类本身测试时,我可以attachments在那里访问。

public $chats;
public function __construct($chats)
{
    $this->chats = $chats;
    echo $chats->attachments // It's working in here, I can get the attachments
}

希望我解释清楚,提前谢谢

干杯!

这里是console.log

在此处输入图像描述

这是use,这是我创建事件时的默认设置

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class GetMessages implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
...
4

0 回答 0