为什么找不到 Laravel 删除()方法>

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

Why is Laravel trashed() method not found>

phplaraveleloquentsoft-delete

提问by user1462432

I'm trying to use the soft delete functionality of Elequent ORM in Laravel 4.1

我正在尝试在 Laravel 4.1 中使用 Elequent ORM 的软删除功能

Deleting records works as expected, however when I search for results using withTrashed() and then check to see if it was a soft deleted record using trashed() I get the following error

删除记录按预期工作,但是当我使用 withTrashed() 搜索结果然后使用trashed() 检查它是否是软删除的记录时,我收到以下错误

Call to undefined method Illuminate\Database\Eloquent\Collection::trashed()

调用未定义的方法 Illuminate\Database\Eloquent\Collection::trashed()

Here is my code. Any suggestions?

这是我的代码。有什么建议?

$product = Product::withTrashed()->where('url', Input::get("product_url.$key"))->where('prolist_id', $list->id)->get();

if($product->trashed())
{
    $product->restore();
}

采纳答案by Joel Hinz

get()is returning a collection of objects. If you only want one result, you can do first()instead and call trashed()on that. If you want several, you'll have to call the method individually for each item in a loop.

get()正在返回一个对象集合。如果您只想要一个结果,则可以first()改为执行并调用trashed()该结果。如果需要多个,则必须为循环中的每个项目单独调用该方法。