代码之家  ›  专栏  ›  技术社区  ›  sterfry68

Laravel软删除restore()错误

  •  48
  • sterfry68  · 技术社区  · 11 年前

    以下软删除代码适用于我:

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

    我被难住了。到目前为止,谷歌没有任何帮助。

    2 回复  |  直到 11 年前
        1
  •  103
  •   Brian Dillingham    11 年前

    错误说明 $post 是一个非对象,Laravel不会在没有 withTrashed()

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

    Laravel Docs - Soft Deleting

    当查询使用软删除的模型时,“已删除”模型将不包括在内。。。

        2
  •  5
  •   miken32 Amit D    5 年前

    另一个选项是在废弃模型中搜索特定ID:

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