在Laravel中推动会议问题
每个项目都包含一个插件列表作为复选框。每次我检查一个插件时,我都想将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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最初,您正在制作一个关联数组,其中
item_id
是关键,但是当您在addons
中附加时,您没有指定哪个item_id
<代码> addons 应该更新数组,因此您必须更改此类内容:
Initially you are making an associative array where
item_id
is the key, but when you are appending inaddons
you are not specifying against whichitem_id
theaddons
array should be updated, so you will have to changeto something like this: