Depends... sometimes I make an AJAX cal from Vue back to PHP; sometimes, if appropriate, I will inline a small JSON string as an data- attribute; and sometimes for very large JSON, I will assign it inside tags at the end of my HTML (which I really hate to do!!!)
Jan 12, 2016
9
Level 51
Pass PHP to Vue
How does one pass anything PHP related to VueJS?
Level 51
Figured it out, after having a complete break from it and thinking about it.
I did the following.
Added this to my controller:
// Basically get the cart total and check if the amount is less then 75 then add the delivery charge otherwise continue.
public function getCartTotal() {
if (Cart::total() < 75) {
$cartTotal = Cart::total() + Config::get('moltincart.postage');
return response()->json($cartTotal);
}
else {
$cartTotal = Cart::total() + Config::get('moltincart.postage');
return response()->json($cartTotal);
}
}
Added new route Route::any('cartTotal', 'CartController@getCartTotal');
Then added the following to my JS
// The methods
getTotal: function() {
this.$http.get('/cartTotal', function(total) {
this.$set('total', total);
});
},
// Then added a ready method
ready: function() {
this.getTotal();
},
// Then added this to my html
<span v-model="total">Total @{{ total | currency £ }}</span></strong>
And surprise surprise, VueJS has done it again totally amazed how powerful it really is and the more I'm using it the more I understand how it works.
1 like
Please or to participate in this conversation.