1

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

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

3 Answers 3

2

I 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;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Still result is none. I am using vue js version 1.0.20
Do not forgot to add these on top of your page:<script src="cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></…> <script src="cdnjs.cloudflare.com/ajax/libs/vue-resource/0.6.1/…>
0

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

// 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.