Laravel Collections 除了没有按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42432929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:24:29  来源:igfitidea点击:

Laravel Collections except not working as expected

laravelcollections

提问by HymanSlayer94

I create a collection for list of all address of a particular user and call it as:

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

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

I get the output like:

我得到如下输出:

[
        {
            "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"
        }
]

It still returns the fields that I do an except on. What am I doing wrong?

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

I even tried these:

我什至尝试过这些:

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

and

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

回答by Jerodev

exceptonly works when you have a single array with keys, not for a nested array. Your solution with the each function should work, but I think you have to cast $itemto a collection first.

except仅当您有一个带键的数组时才有效,不适用于嵌套数组。您使用 each 函数的解决方案应该可以工作,但我认为您必须先$item转换为集合。

Like so:

像这样:

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

回答by KimWordWax

The exceptmethod works on the outer array/collection i.e. on the keys of the collection. So you'd have to use an eachoperator

except方法适用于外部数组/集合,即集合的键。所以你必须使用each运算符

The code below does not work because the $itemis not a collection, but a model.

下面的代码不起作用,因为$item它不是一个集合,而是一个模型。

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

Solution 1

方案一

One solution to your problem would be to use the selectmethod like this (provided GetAddresses is a real laravel relationship).

您的问题的一种解决方案是使用这样的select方法(前提是 GetAddresses 是真正的 laravel关系)。

$user->GetAddresses()->select(['col1', 'col2'])->get()

$user->GetAddresses()->select(['col1', 'col2'])->get()

Solution 2

解决方案2

Another would be to map your output like so:

另一种方法是像这样映射您的输出:

$addresses = $addresses->map(function ($item, $key) {
    return [
        "id": $item->id,
        "user_id": $item->user_id,
        "name": $item->name,
        "line1": $item->line1,
        // etc...
    ];
});

Solution 3

解决方案3

Yet another solution would be to utilize the $hiddenattribute (see this link) on the model and use to to toArray()on each item. That would hide the attributes you have entered in the $hiddeninstance variable

另一种解决方案是利用模型上的$hidden属性(请参阅此链接)并toArray()在每个项目上使用 to 。这将隐藏您在$hidden实例变量中输入的属性