1

I'm making a simple shopping cart system using Laravel and Vue.js. I can successfully add items to my shopping basket and retrieve them to a certain point, but I'm having trouble actually outputting the items to the view.

When I hit my basket route, I load the basket view which triggers the following Vue code:

new Vue({
  el: '#basket',
  ready: function () {
    this.fetchBasket();
  },
  methods: {
    fetchBasket: function(){
      this.$http.get('api/buy/fetchBasket', function (basketItems) {
        this.$set('basketItems', basketItems);
      });
    }
  }
});

This works and I get data returned in the console:

{027c91341fd5cf4d2579b49c4b6a90da: 
{id: 1, name: "A Gnarly Tree", price: 15, quantity: 2}}
  027c91341fd5cf4d2579b49c4b6a90da:
  {id: 1, name: "A Gnarly Tree", price: 15, quantity: 2}
    id:1
    name:"A Gnarly Tree"
    price:15
    quantity:2

However, nothing appears in the view itself. I'm trying to use v-for to populate a table:

<tbody>
    <tr v-for="item in basketItems">
        <td>@{{ item.name }}</td>
        <td>@{{ item.price }}</td>
        <td>@{{ item.quantity }}</td>
        <td>@<button class="btn btn-primary">Update</button></td>
    </tr>
</tbody>

What am I doing wrong?

2
  • What does the @ before the mustaches do? Commented Jul 13, 2016 at 13:22
  • @gurghet tells Blade (laravel templating engine) to ignore it, otherwise it will treat it as a PHP echo, basically. Commented Jul 13, 2016 at 13:26

1 Answer 1

1

You didn't initialize the basketItems in the data of your vue instance. Try to add:

...
el: '#basket',
data: { basketItems: [] }, // <-- this line
ready: function(){
  this.fetchBasket();
},
...

Also edit

this.$set('basketItems', basketItems);

to

Vue.set(this, 'basketItems', basketItems)

Sign up to request clarification or add additional context in comments.

5 Comments

No luck I'm afraid
Don't use this.$set is deprecated, I edited my answer
Works here: jsfiddle.net/gurghet/dL2m248z/1 what does the basketItems variable contain? are you sure you don't need to display basketItems.data instead?
It must be some problem with the way the data is returned from the controller. As you can see from the question, the actual data is contained in a collection or something with a weird random string.
Well, you can't expect Vue to magically guess a random string. You should access it with something like: Object.keys(basketItems.something)[0]. You should update your question with the content of that basketItems variable

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.