Laravel 软删除 restore() 错误 [英] Laravel Soft Delete restore() Error

查看:19
本文介绍了Laravel 软删除 restore() 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following soft delete code works fine for me:

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

deleted_at 字段已更新.但这给了我一个错误:

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

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

错误如下:

exception 'SymfonyComponentDebugExceptionFatalErrorException' with message 'Call to a member function restore() on a non-object'

我被难住了.到目前为止,Google 没有任何帮助.

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

推荐答案

Error 说 $post 是一个非对象,Laravel 不会在没有 withTrashed() 的情况下返回垃圾记录代码>

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

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

Laravel 文档 - 软删除

查询使用软删除的模型时,不会包含已删除"的模型...

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

这篇关于Laravel 软删除 restore() 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆