Laravel 软删除 restore() 错误

标签 laravel eloquent soft-delete

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

$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/

相关文章:

php - Laravel : How to Update multiple Select option with select2. js

mysql - Laravel DB::raw 读取 where 子句中的第二个参数一个字符串而不是来自另一个表的动态列

php - Laravel `delete()` 影响其他时间戳列

c# - 软删除休眠

php - Laravel s3 多个桶

mysql - 为 where 命令使用表单值

javascript - 无法让 vue-router 与 webpack 一起工作

php - Laravel 在 Eloquent 作用域和查询中使用 select

azure - 查找 Azure 存储中软删除 blob 的永久删除日期

php - Laravel,从末尾选择数据库,但按升序排列