2014-08-29 82 views
9

下面的軟刪除代碼工作正常,我:Laravel軟刪除恢復()錯誤

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

我難倒。谷歌迄今爲止沒有幫助。

回答

20

錯誤說$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...

+0

就像一個魅力返回丟棄記錄。謝謝。 – moreirapontocom 2017-09-18 14:10:41