Thursday, March 1, 2018

Laravel – add to cart ajax issue - development

Laravel – add to cart ajax issue

guys when I try to add item to EMPTY(empty session) cart asynchronously it doesn’t work. But if I refresh the item appears in cart and from there I can add items asynchronously. What am I missing?

controller function:

public function addToBasket(Request $request, $id)
{
    $newBasket = new Basket($previousBasket);
    $newBasket->addProduct($product, $product->id);

    Session::put('basket', $newBasket);
    return response()->json(['added' => Session::get('basket')->quantity], 200);
}

ajax:

var id = $(this).data('id'); 
var url = "/product/add-to-basket/"

$.ajax({
type: "GET",
url: url+id,
dataType: "json",
data: { id: id },

success: function(data) {
  if(data) {
    $('#counter').html(data['added']);
    console.log(data);
  }



from Laravel Questions and Answers https://laravelquestions.com/laravel/laravel-add-to-cart-ajax-issue/
via Lzo Media

No comments:

Post a Comment