I am trying to make a realtime chat in Laravel using Vue and Pusher. I already have done everything but I can´t understand why my component ChatMessages.vue can show {{ message.user }} where I can see that the user has a name but it can´t show {{ message.user.name }}.
Example:
{{ message.user }}
Here are the results
{{ message.user.name }}
Here are the results
This is my code:
ChatMessages.vue
<template>
<ul class="chat">
<li class="left clearfix" v-for="message in messages">
<div class="chat-body clearfix">
<div class="header">
<strong class="primary-font">
{{ message.user.email }}
</strong>
</div>
<p>
{{ message.message }}
</p>
</div>
</li>
</ul>
</template>
<script>
export default {
props: ['messages'],
mounted(){
console.log(this.messages)
}
};
</script>
ChatForm.vue
<template>
<div class="input-group">
<input id="btn-input" type="text" name="message" class="form-control input-sm" placeholder="Type your message here..." v-model="message.message" @keyup.enter="sendMessage">
<span class="input-group-btn">
<button class="btn btn-primary btn-sm" id="btn-chat" @click="sendMessage">
Send
</button>
</span>
</div>
</template>
<script>
export default {
props: ['user'],
data() {
return {
message:{
message: ' ',
user: this.user
}
}
},
methods: {
sendMessage() {
this.$emit('messagesent', this.message)
this.message = {}
}
}
}
</script>
App.js
Vue.component('chat-messages',
require('./components/ChatMessages.vue').default);
Vue.component('chat-form', require('./components/ChatForm.vue').default);
const app = new Vue({
el: '#app',
data: {
messages: []
},
mounted() {
this.fetchMessages();
Echo.private('chat')
.listen('MessageSent', (e) => {
this.messages.push({
message: e.message.message,
user: e.user
});
});
},
methods: {
fetchMessages() {
axios.get('/chat/messages').then(response => {
this.messages = response.data;
})
},
addMessage(message) {
this.messages.push(message);
axios.post('/chat/messages', message).then(response => {
//console.log(response.data);
})
}
}
});
The view
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>HelpEachOthers</title>
<!-- Bulma CDN -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.css" />
<!-- Bootstrap CDN -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
</head>
<body>
<div class="container" id="app">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Chats</div>
<div class="panel-body">
<chat-messages :messages="messages"></chat-messages>
</div>
<div class="panel-footer">
<chat-form v-on:messagesent="addMessage" user="{{ Auth::user() }}"></chat-form>
</div>
</div>
</div>
</div>
</div>
<script src="{{ URL::asset('js/app.js') }}"></script>
</body>
</html>
This is only happening in new messages that are sent after the page has loaded. The messages that were in the database when the component is mounted are displayed with the correct name tag. For test this I have 2 chrome windows with 2 differentes users. In the windows where I am sending the message I can´t see the name but in the other window with other user I can see the name in realtime, without reloading.


Auth::user()return? If it's some sort of JSON object, shouldn't you be binding theuserprop attribute, iev-bind:user="{{ Auth::user() }}"?