在Laravel中推动会议问题

发布于 2025-02-10 11:04:01 字数 2569 浏览 3 评论 0原文

每个项目都包含一个插件列表作为复选框。每次我检查一个插件时,我都想将addon_ID推入插件数组。它将Addon_ID推到会话上,但无法正确推动它。

此代码将项目添加到购物车(注意:我正在创建一个空的插件数组):

$cart = session()->get('cart', []);
    
if(isset($cart[$request->item_id])) {
    
  $cart[$request->item_id]['qty']++;
    
} else {
    
   $cart[$request->item_id] = [
    
      'id' => $request->item_id,
      'category_id' => $request->category_id,
      'price' => $request->item_price,
      'item_name' => $request->item_name,
      'qty' => 1,
      'addons' => [],
    
   ];
    
}

session()->put('cart', $cart);

此代码将插件推到项目:

if(isset($request->item_id)) {
    
   session()->push('cart.addons', $request->addon_id);
    
}
            
$cart = session()->get('cart', []);
                
return response()->json([
    
   'message' => 'Addon added',
   'cart' => $cart,
    
]);

这是现在推动插件的方式:

Array
(
    [59] => Array
        (
            [id] => 59
            [category_id] => 27
            [price] => 3.78
            [item_name] => Simple Sandwich
            [qty] => 1
            [addons] => Array
                (
                )
        )

    [57] => Array
        (
            [id] => 57
            [category_id] => 27
            [price] => 5.99
            [item_name] => Cheese Burger
            [qty] => 1
            [addons] => Array
                (
                )
        )

    [addons] => Array
        (
            [0] => 31
        )

    [addons] => Array
        (
            [0] => 61,
            [1] => 60,
            [2] => 31
        )
)

< < Strong>我需要像这样推动它们:

Array
(
    [59] => Array
        (
            [id] => 59
            [category_id] => 27
            [price] => 3.78
            [item_name] => Simple Sandwich
            [qty] => 1
            [addons] => Array
                (
                  [0] => 31
                )
        )

    [57] => Array
        (
            [id] => 57
            [category_id] => 27
            [price] => 5.99
            [item_name] => Cheese Burger
            [qty] => 1
            [addons] => Array
                (
                  [0] => 61,
                  [1] => 60,
                  [2] => 31
                )
        )
)

我试图找到一个解决方案,但似乎没有什么能像我需要的方式一样。

非常感谢帮助。

Each item contains a list of addons as checkboxes. Every time I check an addon I want to push addon_id into the addons array. It's pushing an addon_id to session but it's not pushing it properly.

This code adds item to cart (Notice: I am creating an empty addons array):

$cart = session()->get('cart', []);
    
if(isset($cart[$request->item_id])) {
    
  $cart[$request->item_id]['qty']++;
    
} else {
    
   $cart[$request->item_id] = [
    
      'id' => $request->item_id,
      'category_id' => $request->category_id,
      'price' => $request->item_price,
      'item_name' => $request->item_name,
      'qty' => 1,
      'addons' => [],
    
   ];
    
}

session()->put('cart', $cart);

And this code pushes addons to items:

if(isset($request->item_id)) {
    
   session()->push('cart.addons', $request->addon_id);
    
}
            
$cart = session()->get('cart', []);
                
return response()->json([
    
   'message' => 'Addon added',
   'cart' => $cart,
    
]);

This is how addons are being pushed now:

Array
(
    [59] => Array
        (
            [id] => 59
            [category_id] => 27
            [price] => 3.78
            [item_name] => Simple Sandwich
            [qty] => 1
            [addons] => Array
                (
                )
        )

    [57] => Array
        (
            [id] => 57
            [category_id] => 27
            [price] => 5.99
            [item_name] => Cheese Burger
            [qty] => 1
            [addons] => Array
                (
                )
        )

    [addons] => Array
        (
            [0] => 31
        )

    [addons] => Array
        (
            [0] => 61,
            [1] => 60,
            [2] => 31
        )
)

I need them to be pushed like this:

Array
(
    [59] => Array
        (
            [id] => 59
            [category_id] => 27
            [price] => 3.78
            [item_name] => Simple Sandwich
            [qty] => 1
            [addons] => Array
                (
                  [0] => 31
                )
        )

    [57] => Array
        (
            [id] => 57
            [category_id] => 27
            [price] => 5.99
            [item_name] => Cheese Burger
            [qty] => 1
            [addons] => Array
                (
                  [0] => 61,
                  [1] => 60,
                  [2] => 31
                )
        )
)

I tried to find a solution but nothing seems to be working the way I need it.

Help is very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两仪 2025-02-17 11:04:01

最初,您正在制作一个关联数组,其中item_id是关键,但是当您在addons中附加时,您没有指定哪个item_id <代码> addons 应该更新数组,因此您必须更改

if(isset($request->item_id)) {
    
   session()->push('cart.addons', $request->addon_id);
    
}

此类内容:

if(isset($request->item_id)) {
   $cart = session()->get('cart');
   if(isset($cart[$request->item_id]) {
   array_push($cart[$request->item_id]['addons'],$request->addon_id);
   session()->put('cart',$cart);
 }
}

Initially you are making an associative array where item_id is the key, but when you are appending in addons you are not specifying against which item_id the addons array should be updated, so you will have to change

if(isset($request->item_id)) {
    
   session()->push('cart.addons', $request->addon_id);
    
}

to something like this:

if(isset($request->item_id)) {
   $cart = session()->get('cart');
   if(isset($cart[$request->item_id]) {
   array_push($cart[$request->item_id]['addons'],$request->addon_id);
   session()->put('cart',$cart);
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文