dor2p0520
2017-01-25 11:46
浏览 135
已采纳

Laravel - 如何在laravel会话中更新数组值?

I'm creating cart functionality in laravel 5.3. when user add product to cart it's basically create session and add product to that session.

let's start with example on that I'm working.

when user add product to cart I am passing required parameter to controller function and then it's store in session. so my controller function is :

public function add_cart(Request $request){


    $product = [
        'sku' => $request['sku'],
        'price' => $request['price'],
        'quantity' => $request['quantity'],
        'brand_id' => $request['brand_id'],
        'brand_name' => $request['brand_name'],
        'brand_img' => $request['brand_img']
    ];

   // dd($product);
    if($request->session()->has('add_to_cart')){
        $request->session()->push('add_to_cart', $product);
    }else{
        $request->session()->push('add_to_cart', $product);
    }


    return count($request->session()->get('add_to_cart'));

}

so after add product to add_to_cart session my session is look like this :

array:2 [▼
0 => array:6 [▼
"sku" => ""
"price" => " "
"quantity" => "1"
"brand_id" => "14"
"brand_name" => "The Body Shop"
"brand_img" => "http://s3.giftcardsindia.in/2015/06/01/original-556c623c560f5.jpg"
 ]
1 => array:6 [▼
"sku" => "ffs002000"
"price" => "2000"
"quantity" => "1"
"brand_id" => "47"
"brand_name" => "The Four Fountains Spa"
"brand_img" => "http://s3.giftcardsindia.in/2015/06/01/original-556c47b5a27eb.jpg"
]
]

using this I can show all cart product in cart page also done with subtotal , tax and total of payment functionality.

Now problem is if I want to update quantity of brand_id : 14 how can I get array of product from session where brand_id is 14 ? and after geting that product array how to update only quantity value from that array ? or may be we can replace whole product array with new one but how to find that product array from session ?

don't have any idea. I search on laravel documentation but not get any method to solve my problem.

图片转代码服务由CSDN问答提供 功能建议

我在laravel 5.3中创建购物车功能。 当用户将产品添加到购物车时,它基本上是创建会话并将产品添加到该会话。

让我们以我正在工作的示例开始。

当用户将产品添加到购物车时,我将所需的参数传递给控制器​​功能,然后将其存储在会话中。 所以我的控制器功能是:

  public function add_cart(Request $ request){
 
 
 $ product = [
'sku'=>  $ request ['sku'],
'price'=>  $ request ['price'],
'数量'=>  $ request ['quantity'],
'brand_id'=>  $ request ['brand_id'],
'brand_name'=>  $ request ['brand_name'],
'brand_img'=>  $ request ['brand_img'] 
]; 
 
 // dd($ product); 
 if($ request-> session() - > has('add_to_cart')){
 $ request  - > session() - > push('add_to_cart',$ product); 
} else {
 $ request-> session() - > push('add_to_cart',$ product); 
}  
 
 
返回计数($ request-> session() - > get('add_to_cart')); 
 
} 
   
 
 

所以在将产品添加到 add_to_cart 会话后,我的会话看起来像这样:

  array:2 [▼
0 => 数组:6 [▼
“sku”=>  “”
“price”=>  “”
“数量”=>  “1”
“brand_id”=>  “14”
“brand_name”=>  “The Body Shop”
“brand_img”=>  “http://s3.giftcardsindia.in/2015/06/01/original-556c623c560f5.jpg"
] 
1 => 数组:6 [▼
“sku”=>  “ffs002000”
“price”=>  “2000”
“数量”=>  “1”
“brand_id”=>  “47”
“brand_name”=>  “The Four Fountains Spa”
“brand_img”=>  “http://s3.giftcardsindia.in/2015/06/01/original-556c47b5a27eb.jpg"
]
]
nn

使用此我可以 显示购物车页面中的所有购物车产品也包含小计,税金和支付功能总额。

现在问题是我是否要更新 brand_id的数量:14 如何从brand_id为14的会话中获取产品数组? 在确定该产品数组之后,如何仅更新该数组中的数量值? 或者我们可以用新的替换整个产品阵列,但如何从会话中找到该产品阵列?

不知道。 我搜索laravel文档,但没有得到任何方法来解决我的问题。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongliu5475 2017-01-25 11:52
    已采纳

    There is no direct way of doing that, You can crate a simple function to update the session for you..

    Here is an example.

    public function updateQuantityByBrand($brand_id, $quantity){
       $cartItems = session()->get('add_to_cart');
    
       foreach($cartItems as $item){
          if($item["brand_id"] == $brand_id){
              $item["quantity"] = $quantity;
          }
       }
    
       return session("add_to_cart", $cartItems);
    }
    
    打赏 评论

相关推荐 更多相似问题