繁体   English   中英

Laravel 软删除 restore() 错误

[英]Laravel Soft Delete restore() Error

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

$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 文档 - 软删除

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

另一种选择是在垃圾模型中搜索特定 ID:

Post::onlyTrashed()->where('id', $post_id)->restore();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM