Laravel如何使用$请求,除了嵌套数组
是否可以在嵌套数组上使用 $ request->除外?这是请求数据:
[▼
"id" => 1
"products" => array:1 [▼
0 => array:4 [▼
"id" => 1
"name" => "sample product"
"date" => "07/04/2022"
"frequency" => array:1 [ …1]
]
]
]
我的目标是删除一些钥匙值对,例如删除 id , date 和频率。我所需的结果是:
[▼
"id" => 1
"products" => array:1 [▼
0 => array:1 [▼
"name" => "sample product"
]
]
]
到目前为止,我尝试的是使用 arr:除函数外:
$request->products = Arr::except($request->products['0'], ['id', 'date', 'frequency']);
但这应该应用于所有项目。假设我有两个项目产品,所需的结果是:
[▼
"id" => 1
"products" => array:2 [▼
0 => array:1 [▼
"name" => "sample product"
]
1 => array:1 [▼
"name" => "sample product 2"
]
]
]
需要您对最佳方法的投入。谢谢你!
Is it possible to use $request->except on a nested array? This is the request data:
[▼
"id" => 1
"products" => array:1 [▼
0 => array:4 [▼
"id" => 1
"name" => "sample product"
"date" => "07/04/2022"
"frequency" => array:1 [ …1]
]
]
]
My goal is to remove some key value pairs e.g. removing id, date and frequency. My desired result would be:
[▼
"id" => 1
"products" => array:1 [▼
0 => array:1 [▼
"name" => "sample product"
]
]
]
What I've tried so far is to use Arr:except function:
$request->products = Arr::except($request->products['0'], ['id', 'date', 'frequency']);
But this should be applied on all items. Let's say I have two item products, the desired result would be:
[▼
"id" => 1
"products" => array:2 [▼
0 => array:1 [▼
"name" => "sample product"
]
1 => array:1 [▼
"name" => "sample product 2"
]
]
]
Need your inputs on what's the best approach for this. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非我缺少明显的东西,否则您能否不仅在
$ replect olection
对象中迭代products
?导致
$过滤
包含:更新1
而不确定您的意思是上述原则对我有用。
如果我提供以下内容:
然后进行
dd($ request-> products);
我得到:Unless I am missing something obvious, could you not just iterate over the
products
in your$request
object?Resulting in
$filtered
containing:Update 1
Not sure what you mean by this as the above principle works for me.
If I supply the above with the following:
Then do a
dd($request->products);
I get: