Data Fetching From Database Using Laravel And Vuejs
I am trying to fetching data from database using Laravel and Vue.js. Result I am getting is none. But firebug console shows the response. Please find the attached image. Please ch
Solution 1:
I have edited the controller part. Now I got result.
Replace return response()->json(['messages' => $listCustomers])
with return $listCustomers
.
public function showCustomersData()
{
$listCustomers = Customer::select('id', 'name', 'reference_id')
->orderBy('id', 'desc')
->get();
return $listCustomers;
}
Solution 2:
You may need to pre-initialise the messages variable in the Vue data object as per the warning message in your console. I think it depends on the version of VueJs you're using. Try:
new Vue({
el: '#app',
data: {
messages: []
},
ready: function() {
this.fetchMessages();
},
methods: {
fetchMessages: function() {
this.$http.get('/customers/list/data', function(messages) {
alert(messages);
this.$set('messages', messages);
});
}
}
});
Solution 3:
Here is an example using simulated ajax request to update grid data. Hopes to help you.
var data = [{
title: 'Test',
description: 'This is a test.'
}, {
title: '404Error',
description: '404 Page not found.'
}];
new Vue({
el: '#app',
data: {
messages: []
},
ready: function() {
this.fetchMessages();
},
methods: {
fetchMessages: function() {
var self = this;
// simulate the ajax request
setTimeout(function(){
self.messages = data;
}, 1000);
}
}
});
table {
border: 1px solid #ccc;
table-layout: fixed;
border-collapse: separate;
}
table th, table td {
border: 1px solid #ccc;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.20/vue.min.js"></script>
<div id="app">
<table>
<tr>
<th>Index</th>
<th>Title</th>
<th>Description</th>
<th>Action</th>
</tr>
<tr v-for="message in messages">
<td>{{$index+1}}</td>
<td>{{message.title}}</td>
<td>{{message.description}}</td>
<td><button>Click me</button></td>
</tr>
</table>
</app>
P.S. The first warning tells you to change your code, it prefers
// GET request
this.$http({url: '/someUrl', method: 'GET'}).then(function (response) {
// success callback
}, function (response) {
// error callback
});
The second warning tells you to pre-init the variable messages
in data
.
The last, sorry I don't know, haven't see it before.
Post a Comment for "Data Fetching From Database Using Laravel And Vuejs"