admin管理员组文章数量:1332872
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 check my code and correct me.
data.blade.php
@extends('layouts.app')
@section('title', 'Customers List')
@section('styles')
@endsection
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Customers List</div>
<div class="panel-body">
<table class="table table-hover table-bordered" id="app">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Reference ID</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
?>
<tr v-for="message in messages">
<td> {{ $i }} </td>
<td> @{{ message.name }} </td>
<td> @{{ message.reference_id }} </td>
<td><a href="/customers/list/process/" class="btn btn-primary btn-xs" role="button">Click Here</a></td>
</tr>
<?php $i++; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
<script src="/js/vue.js"></script>
<script src="/js/vue-resource.min.js"></script>
<script src="/js/customers.js"></script>
@endsection
customers.js
new Vue({
el: '#app',
ready: function() {
this.fetchMessages();
},
methods: {
fetchMessages: function() {
this.$http.get('/customers/list/data', function(messages) {
alert(messages);
this.$set('messages', messages);
});
}
}
});
Controller
public function showCustomersData()
{
$listCustomers = Customer::select('id', 'name', 'reference_id')
->orderBy('id', 'desc')
->get();
return response()->json(['messages' => $listCustomers]);
}
Route
Route::get('/customers/list/data', [
'as' => 'customerListData', 'uses' => 'CustomerController@showCustomersData'
]);
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 check my code and correct me.
data.blade.php
@extends('layouts.app')
@section('title', 'Customers List')
@section('styles')
@endsection
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Customers List</div>
<div class="panel-body">
<table class="table table-hover table-bordered" id="app">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Reference ID</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
?>
<tr v-for="message in messages">
<td> {{ $i }} </td>
<td> @{{ message.name }} </td>
<td> @{{ message.reference_id }} </td>
<td><a href="/customers/list/process/" class="btn btn-primary btn-xs" role="button">Click Here</a></td>
</tr>
<?php $i++; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
<script src="/js/vue.js"></script>
<script src="/js/vue-resource.min.js"></script>
<script src="/js/customers.js"></script>
@endsection
customers.js
new Vue({
el: '#app',
ready: function() {
this.fetchMessages();
},
methods: {
fetchMessages: function() {
this.$http.get('/customers/list/data', function(messages) {
alert(messages);
this.$set('messages', messages);
});
}
}
});
Controller
public function showCustomersData()
{
$listCustomers = Customer::select('id', 'name', 'reference_id')
->orderBy('id', 'desc')
->get();
return response()->json(['messages' => $listCustomers]);
}
Route
Route::get('/customers/list/data', [
'as' => 'customerListData', 'uses' => 'CustomerController@showCustomersData'
]);
Share
Improve this question
edited Nov 17, 2017 at 3:04
Jeff Puckett
41.1k19 gold badges124 silver badges174 bronze badges
asked Mar 30, 2016 at 8:39
Sarath TSSarath TS
2,4626 gold badges36 silver badges79 bronze badges
3 Answers
Reset to default 2I 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;
}
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);
});
}
}
});
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./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.
本文标签: javascriptData fetching from database using laravel and vuejsStack Overflow
版权声明:本文标题:javascript - Data fetching from database using laravel and vuejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742297168a2448967.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论