I cannot get the data from the model
with a relationship. But when I use return response()->json($chat->load('attachments'))
I can get the attachments.
I want to get data Chat
data with the attachments
.
This is my Model Chat.php
class Chat extends Model
{
public function attachments()
{
return $this->hasMany('App\ChatAttachment');
}
}
And this is my controller 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);
}
My event 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';
}
}
And my 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
})