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

查看:53
本文介绍了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 '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.

推荐答案

错误提示 $ 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天全站免登陆