Laravel 软删除 restore() 错误

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

Laravel Soft Delete restore() Error

laraveleloquentsoft-delete

提问by sterfry68

The following soft delete code works fine for me:

以下软删除代码对我来说很好用:

$post = Post::find($post_id);
$post->delete();

The deleted_at field is updated. But this gives me an error:

Deleted_at 字段已更新。但这给了我一个错误:

$post = Post::find($post_id);
$post->restore();

Here's the error:

这是错误:

exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function restore() on a non-object'

I'm stumped. Google is no help so far.

我难住了。到目前为止,谷歌没有帮助。

回答by Brian Dillingham

Error says $postis a non-object, Laravel doesn't return trashed records without withTrashed()

错误说$post是一个非对象,Laravel 不会返回垃圾记录withTrashed()

Post::withTrashed()->find($post_id)->restore();

Laravel Docs - Soft Deleting

Laravel 文档 - 软删除

When querying a model that uses soft deletes, the "deleted" models will not be included...

查询使用软删除的模型时,将不包括“已删除”的模型...