0 down vote favorite I am using this code in my LARAVEL project
http://jsfiddle.net/teepluss/12wqxxL3/
Items are dynamically generated in the cart_items array.
I want to know how to loop over the generated items and post this to the database or how to pass this to the controller and save it from there.
How should i go about doing that
Here is my script
var app = new Vue({
http: { root: '/root', headers: { 'X-CSRF-TOKEN': document.querySelector('#token').getAttribute('value') } },
el: '#wrapper', data: { cart_items: [], },
created() {
this.$http.get('http://localhost:8000/api/ice', function(ice) {
//console.log(ice);
this.articles = ice;
}.bind(this));
},
computed: {
count: function() {
return this.cart_items.length;
},
total: function() {
return _.reduce(this.cart_items, function(n, cart_item) {
return cart_item.price * cart_item.qty + n;
}, 0).toFixed(2);
},
filteredArticles: function() {
var articles_array = this.articles,
searchString = this.searchString;
if (!searchString) {
return articles_array;
}
searchString = searchString.trim().toLowerCase();
articles_array = articles_array.filter(function(item) {
if (item.title.toLowerCase().indexOf(searchString) !== -1) {
return item;
}
})
return articles_array;;
}
},
methods: {
notInCart: function(article) {
var ids = _.pluck(this.cart_items, 'id');
return !_.contains(ids, article.id);
},
addToCart: function(product) {
var cart = Vue.util.extend({}, product);
var ids = _.pluck(this.cart_items, 'id');
if (!_.contains(ids, product.id)) {
cart.qty = 1;
this.cart_items.push(cart);
}
},
addQty: function(product) {
product.qty = product.qty +1;
},
decQty: function(product) {
product.qty -= 1;
if (product.qty <= 0) {
this.cart_items.$remove(product);
}
},
clearall:function(){
this.cart_items = [];
},
UserOrder: function(){
var order = this.cart_items;
var output = JSON.parse(JSON.stringify(order));
// I am able to see the output in the dev console here
// How should go about posting this to the mysql database.
console.log(output);
}
}
});
Also here is project settings
Laravel - 5.4 vue - 1.0.12 vue resource - 0.1.17
I am new to this. Any help would be greatly appreciated
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community