简体   繁体   中英

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.

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.

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

async: false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM