eyes_only
eyes_only

Reputation: 11

Update array in session in Laravel 5.5

I'm using Laravel 5.5.19 and recently encountered an unpleasant situation. I have a few forms generated in blade template:

    @foreach($products as $product)

    <form id="product{{$product['id']}}" class="cartitem">
        <input type="hidden" class="cartitemid" name="cartitemid" value="{{$product['id']}}">
        <input type="text" class="count" name="count" value="{{$product['count']}}">
        <input type="hidden" name="cartid" value="{{$product['cartid']}}">
        {{ csrf_field() }}
    </form>

    @endforeach
<button id="refresh"  type="button" class="btn btn-danger" name="refresh" onClick="refresh(this);">Refresh</button> 

And that forms serialized and sent to controller via Jquery:

function refresh(btn){
    btn.blur();
    if ($('.cartitem').length) {
    $(".cartitem").each(function(){
        var data = $(this).serialize();
        $.ajax({
            url: '/cart/refresh',
            type: 'POST',
            data: data,
            success: function (response) {
              var str = JSON.stringify(response.cart, null, 2);
            console.log(response.id);
            console.log(response.count);
            console.log(str);
            }
        });
        location.reload();
});
}
}

Controller code:

    public function refresh(Request $request){  
    if (session()->has('cartitems')) {  
    $validatedData = $request->validate([
                   'cartitemid' => 'required|numeric',
                   'count' => 'required|numeric|between:1,255',
                   'cartid' => 'required|string|max:10',
                ]);
            $cartitems = $request->session()->get('cartitems');

                    $count = $validatedData['count'];
                    $id = $validatedData['cartitemid'];
                            foreach ($cartitems as $cartitem => $value){
                                if ($value['id'] == $id){
                                    $cartitems[$cartitem]['count'] = $count;

                                }
                            }
$response = array(
          'cart' => $cartitems,
          'count' => $count,
          'id' => $id,
      );

                  $request->session()->forget('cartitems');
                  $request->session()->put('cartitems', $cartitems);
                  return response()->json($response);
    }
    }

For some reason it's not working. Not saving array in some iterations. This also doesn't work with database or cookie driver.

UPDATE Code is working. I'm getting output in the console, like this:

1
1
1
[
  {
    "id": 1,
    "productid": "1",
    "count": "1",
    "price": 10001.2
  }
]

But in the session is still an old array.

Upvotes: 0

Views: 921

Answers (2)

eyes_only
eyes_only

Reputation: 11

Well, I kinda solved my question. Very simple just adding in ajax request.

async: false

Upvotes: 0

J&#233;r&#233;myCasper
J&#233;r&#233;myCasper

Reputation: 182

The issue seems to be here

return response()->json($response);
              $request->session()->forget('cartitems');
              $request->session()->put('cartitems', $cartitems);

You run the return statement before the $request->session() so the code never go there.

Just set the return line at the end.

Upvotes: 1

Related Questions