【问题标题】:Data fetching from database using laravel and vuejs使用 laravel 和 vuejs 从数据库中获取数据
【发布时间】:2016-07-18 03:43:00
【问题描述】:

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

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);
            });
        }
    }
});

控制器

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'
    ]);

【问题讨论】:

    标签: javascript php laravel vue.js


    【解决方案1】:

    您可能需要根据控制台中的警告消息预先初始化 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);
                });
            }
        }
    });
    

    【讨论】:

    • 仍然没有结果。我正在使用 vue js 版本 1.0.20
    • 不要忘记在页面顶部添加这些:
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      附: 第一个警告告诉您更改代码,它更喜欢

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

      第二个警告告诉您在data 中预初始化变量messages
      最后一个,不好意思不知道,之前没看过。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-14
        • 2019-12-30
        • 1970-01-01
        • 2018-02-11
        • 1970-01-01
        • 2020-08-17
        • 1970-01-01
        相关资源
        最近更新 更多