javascript - 使用laravel和vuejs从数据库获取数据

标签 javascript php laravel vue.js

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

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::get('/customers/list/data', [
        'as' => 'customerListData', 'uses' => 'CustomerController@showCustomersData'
    ]);

最佳答案

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

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

关于javascript - 使用laravel和vuejs从数据库获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36303720/

相关文章:

javascript - 使用 KnockoutJS 初始化后更新引导日期选择器

php - 创建一个基于简单文本文件的搜索引擎

javascript - Laravel 7.5.2(护照 Api)+ Ajax 错误 : Undefined index: aud ,\\vendor\\laravel\\passport\\src\\Guards\\TokenGuard.php

php - 在 Laravel MongoDB 中显示 EmbedsMany 数据

javascript - 有什么方法可以更改应用程序脚本的用户代理吗?

javascript - promise 取消方法。为什么与其他 Promise 框架相比它还没有实现?

javascript - 数组foreach困惑

javascript - 如何使用一些前缀进行自动增量

php - CakePHP:从 View 中的输出创建可点击的 URL

php - AWS SQS 上的 Laravel 队列 worker - 大量请求?