本文介绍了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文档-软删除

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

08-15 14:32