laravel 忘记不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20735181/
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 08:47:51  来源:igfitidea点击:

Forget doesn't work

collectionslaravel

提问by vblinden

If I try to remove an item from this collection

如果我尝试从这个集合中删除一个项目

$examples = Example::where('example', '=', $data['example'])->get();

by doing

通过做

$examples->forget(20);

it doesn't remove the item from the collection, I still get back all the items that were in there originally. I have read the Laravel documentation and the api docs. And it should work (I think) but it doesn't.

它不会从集合中删除项目,我仍然会取回原来在那里的所有项目。我已阅读 Laravel 文档和 api 文档。它应该工作(我认为)但它没有。

Could someone point me out what I am doing wrong here?

有人能指出我在这里做错了什么吗?

This still returns an object.

这仍然返回一个对象。

$examples->forget(49);
return $examples->find(49);

P.S. Ever other method like push or get works.

PS 有没有其他方法,如 push 或 get 工作。

Thanks alot!

非常感谢!

采纳答案by Anam

You did a small mistake, actually you didn't notice that. I did myself :).

你犯了一个小错误,其实你没有注意到。我自己做了:)。

Forgetuse the array keyto delete an object item from collection.

Forget使用array key删除集合中的对象项。

Array(0 => 'abc'
 1 => 'bcd'
 49 => 'aaa'
)

$examples->forget(49);
                  ^^ array key 49

Where as, finduse the idto find an object from an collection

其中,find使用id从集合中查找对象

table: examples

id example
1   abc
49  bce

$examples->find(49);
                ^^ `example id`

回答by Tyler Arbon

I just wanted to add on to the Anam's answer. Once you have a collection you can then loop through it like this to delete by ID

我只是想补充一下 Anam 的回答。一旦你有了一个集合,你就可以像这样循环遍历它以按 ID 删除

function forgetById($collection,$id){
    foreach($collection as $key => $item){
        if($item->id == $id){
            $collection->forget($key);
            break;
        }
    }
    return $collection;
}

$examples = Example::where('example', '=', $data['example'])->get();

$examples = forgetById($examples,20);

回答by rickshawhobo

According to the documentation it says that forget()works by key. The word "key" is ambiguous because what they mean to say is array key also known as "index" and not model key which is the id.

根据文档,它说这forget()是按密钥工作的。“ key”这个词是模棱两可的,因为它们的意思是数组键也称为“索引”,而不是模型键,即id。

However, in the other methods such as find()or contains()they use the word "key" to mean model key so you can see the confusion.

但是,在其他方法中,例如find()orcontains()他们使用单词“ key”来表示模型键,因此您可以看到混淆。

When you look at the source you can see that the forget()method is found in the Illuminate\Support\Collectionclass and not in Illuminate\Database\Eloquent\Collection.

当您查看源代码时,您会发现该forget()方法是在Illuminate\Support\Collection类中找到的,而不是在Illuminate\Database\Eloquent\Collection.

My theory is that the support class is supposed to be more generic so it doesn't consider the model keys, but really I don't know.

我的理论是支持类应该更通用,所以它不考虑模型键,但我真的不知道。

回答by Ali Baker

you could do

你可以

$examples->reject(20);

Like most other collection methods, reject returns a new collection instance; it does not modify the collection it is called on.

与大多数其他集合方法一样,reject 返回一个新的集合实例;它不会修改它被调用的集合。