Jac*_*r94 2 collections laravel
我为特定用户的所有地址列表创建了一个集合,并将其命名为:
$addresses = collect($user->GetAddress);
$addresses = $addresses->where('deleted', 0);
$addresses = $addresses->except(['deleted', 'created_at', 'updated_at']);
Run Code Online (Sandbox Code Playgroud)
我得到的输出如下:
[
{
"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"
}
]
Run Code Online (Sandbox Code Playgroud)
它仍然返回我做的字段,除了.我究竟做错了什么?
我甚至试过这些:
$addresses = $addresses->each(function ($item, $key) {
return $item->except(['deleted', 'created_at', 'updated_at']);
});
Run Code Online (Sandbox Code Playgroud)
和
$addresses = $addresses->each->except(['deleted', 'created_at', 'updated_at']);
Run Code Online (Sandbox Code Playgroud)
except
仅在具有键的单个数组时才有效,而不适用于嵌套数组.使用每个函数的解决方案应该可行,但我认为您必须首先$item
转换为集合.
像这样:
$addresses = $addresses->map(function ($item, $key) {
return collect($item)->except(['deleted', 'created_at', 'updated_at'])->toArray();
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4032 次 |
最近记录: |