以下软删除代码对我来说很好用:
$post = Post::find($post_id);
$post->delete();
Deleted_at 字段已更新。但这给了我一个错误:
$post = Post::find($post_id);
$post->restore();
这是错误:
exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function restore() on a non-object'
我难住了。到目前为止,谷歌没有帮助。
最佳答案
错误提示 $post
是一个非对象,Laravel 不会在没有 withTrashed()
的情况下返回垃圾记录
Post::withTrashed()->find($post_id)->restore();
Laravel Docs - Soft Deleting
When querying a model that uses soft deletes, the "deleted" models will not be included...
关于Laravel 软删除 restore() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25576024/