gpt4 book ai didi

Laravel Collections 除了不能按预期工作

转载 作者:行者123 更新时间:2023-12-03 13:44:03 25 4
gpt4 key购买 nike

我为特定用户的所有地址列表创建了一个集合,并将其称为:

$addresses = collect($user->GetAddress);
$addresses = $addresses->where('deleted', 0);
$addresses = $addresses->except(['deleted', 'created_at', 'updated_at']);

我得到如下输出:
[
{
"id": 1,
"user_id": 1,
"name": "mr.test",
"line1": "test1",
"line2": "test2",
"pincode": 100,
"city": "test3",
"state": "test4",
"mobile": "test5",
"default": 0,
"deleted": 0,
"created_at": "2017-02-23 09:20:09",
"updated_at": "2017-02-23 09:20:09"
}
]

它仍然返回我执行的字段除外。我究竟做错了什么?

我什至尝试过这些:
$addresses = $addresses->each(function ($item, $key) {
return $item->except(['deleted', 'created_at', 'updated_at']);
});


$addresses = $addresses->each->except(['deleted', 'created_at', 'updated_at']);

最佳答案

except仅当您有一个带键的数组时才有效,不适用于嵌套数组。您对每个函数的解决方案应该有效,但我认为您必须转换 $item先收藏。

像这样:

$addresses = $addresses->map(function ($item, $key) {
return collect($item)->except(['deleted', 'created_at', 'updated_at'])->toArray();
});

关于Laravel Collections 除了不能按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42432929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com