Laravel 更新会话数组中的值

我正在使用 laravel 构建购物车,我想更新会话数组中的数量。

但它对我不起作用。


  foreach($request->session()->get('shopping_cart') as $value){

    if($value['code'] == $request->product_id){

        echo $value['code'];

        $value['quantity'] = $request->qty_val;

        echo $value['quantity'].'<br />';

        break;

    }

  }


  dd($request->session()->get('shopping_cart'));


 array:2 [▼

     "" => array:5 [▼

     "name" => "Fiona Nicholson"

     "code" => null

     "price" => "848"

     "quantity" => "1"

     "image" => "product_pics/1566371659.jpeg"

    ]

   "sdfwef" => array:5 [▼

      "name" => "Quentin Bryant"

      "code" => "sdfwef"

      "price" => "713"

      "quantity" => "1"

      "image" => "product_pics/1566371616.jpg"

    ]

   ]


暮色呼如
浏览 216回答 1
1回答

扬帆大鱼

要改变内部的值会话阵列最好是使用session()->put()带有点符号,而不是通过引用,即传递的:foreach($request->session()->get('shopping_cart') as $key => $value){&nbsp; &nbsp; if($value['code'] == $request->product_id){&nbsp; &nbsp; &nbsp; &nbsp; $request->session()->put("shopping_cart.$key.quantity", $request->qty_val);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}顺便说一句,您可以使用session()帮助程序,如下所示:foreach(session('shopping_cart') as $key => $value){&nbsp; &nbsp; if($value['code'] == $request->product_id){&nbsp; &nbsp; &nbsp; &nbsp; session(["shopping_cart.$key.quantity" => $request->qty_val]);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}使用带有数组作为参数的助手,与session([$key => $value])使用相同session()->put($key, $value)。
打开App,查看更多内容
随时随地看视频慕课网APP