使用 laravel 和 vuejs 从数据库中获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36303720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Data fetching from database using laravel and vuejs
提问by Sarath TS
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.
我正在尝试使用 Laravel 和 Vue.js 从数据库中获取数据。我得到的结果是没有。但萤火虫控制台显示响应。请找到所附的图片。请检查我的代码并纠正我。
data.blade.php
数据刀片.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
客户.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'
]);
回答by Sarath TS
I have edited the controller part. Now I got result.
Replace return response()->json(['messages' => $listCustomers])
with return $listCustomers
.
我已经编辑了控制器部分。现在我得到了结果。替换return response()->json(['messages' => $listCustomers])
为return $listCustomers
。
public function showCustomersData()
{
$listCustomers = Customer::select('id', 'name', 'reference_id')
->orderBy('id', 'desc')
->get();
return $listCustomers;
}
回答by Erguotou
Here is an example using simulated ajax request to update grid data. Hopes to help you.
下面是一个使用模拟 ajax 请求更新网格数据的示例。希望能帮到你。
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
PS 第一个警告告诉你改变你的代码,它更喜欢
// 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.
第二个警告告诉您messages
在data
.
最后一个,抱歉我不知道,之前没看过。
回答by Steve O
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:
您可能需要根据控制台中的警告消息预先初始化 Vue 数据对象中的消息变量。我认为这取决于您使用的 VueJs 版本。尝试:
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);
});
}
}
});