使用 laravel 和 vuejs 从数据库中获取数据

新手上路,请多包涵

我正在尝试使用 Laravel 和 Vue.js 从数据库中获取数据。我得到的结果是没有。但是萤火虫控制台显示响应。请找到所附图片。请检查我的代码并纠正我。 点击这里

数据.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

客户.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);
            });
        }
    }
});

控制器

public function showCustomersData()
    {
        $listCustomers             = Customer::select('id', 'name', 'reference_id')
            ->orderBy('id', 'desc')
            ->get();
        return response()->json(['messages' => $listCustomers]);
    }

路线

Route::get('/customers/list/data', [
        'as' => 'customerListData', 'uses' => 'CustomerController@showCustomersData'
    ]);

原文由 Sarath TS 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 514
2 个回答

我已经编辑了控制器部分。现在我得到了结果。将 return response()->json(['messages' => $listCustomers]) 替换为 return $listCustomers

 public function showCustomersData()
    {
        $listCustomers             = Customer::select('id', 'name', 'reference_id')
            ->orderBy('id', 'desc')
            ->get();
        return $listCustomers;
    }

原文由 Sarath TS 发布,翻译遵循 CC BY-SA 3.0 许可协议

这是一个使用模拟 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>

PS第一个警告告诉你改变你的代码,它更喜欢

// GET request
this.$http({url: '/someUrl', method: 'GET'}).then(function (response) {
    // success callback
}, function (response) {
    // error callback
});

第二个警告告诉您在 --- 中预初始化变量 messages data

最后,对不起,我不知道,以前没见过。

原文由 Erguotou 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题