I have a view composer that I'm using to calculate all the variables that go in the shopping basket (which is a partial that I include a lot) of the app I'm creating. It is essentially a much more complex version of this:
public function compose($view)
{
$giftbox_total_pre = Giftbox::getTotal();
$giftbox_items_total = number_format($giftbox_total_pre, 2);
$view->with(compact(
'giftbox_items_total',
));
}
Then in my routes I have:
View::composer(['layouts.bag', 'shop.basket', 'giftbox.basket'], 'Acme\Composers\BasketComposer');
But, I'm just adding Ajax to my app, and it would be good if after a request I could grab the variables from the View Composer. Is there a way of doing that? Or is the composer the wrong thing to be using in this instance?
For example, to add an item without Ajax, I have a controller function like the following route:
Route::post('/cart/add/{type?}/{quantity?}', 'CartsController@addToCart');
And controller:
public function addToCart($type='subscription', $quantity=1)
{
$product = Product::find(Input::get('id'));
$price = $product->box_price;
Cart::insert([
'id' => $type . $product->id,
'table_id' => $product->id,
'name' => $product->name,
'price' => $price,
'quantity' => $quantity,
'cart_type' => $type,
]);
return Redirect::back()->with('success', 'Added to cart!');
}
First, proper, Laravel project so I may have myself in a bit of a mess! I can Ajax submit to the route and it works fine, but I just can't work out how to get back a decent JSON array of the cart.
giftbox_items_totallike you have it in the view composer as JSON?