我无法从model
与关系中获取数据。但是当我使用时,return response()->json($chat->load('attachments'))
我可以获得附件。
我想Chat
用attachments
.
这是我的模特Chat.php
class Chat extends Model
{
public function attachments()
{
return $this->hasMany('App\ChatAttachment');
}
}
这是我的控制器Chat.php
public function storeMessage(Request $request)
{
DB::transaction(function() use ($request) {
$chat = new Chat;
$chat->message = $request->message;
$chat->save();
event( new GetMessages($chat->load('attachments')));
}, 2);
}
我的活动GetMessages.php
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';
}
}
还有我的 JS
Echo.channel('get-messages').listen('.get-messages', (data) => {
console.log(data.chats) // Can see the chats object, but no attachments
console.log(data.chats.attachments); // Undefined
})