43

次のソフト削除コードは私にとってはうまくいきます:

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

私は困惑しています。Google は今のところ役に立ちません。

4

2 に答える 2

89

エラー$postは、オブジェクトではないことを示しています。Laravel は、破棄されたレコードを返しません。withTrashed()

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

Laravel Docs - ソフト削除

論理的な削除を使用するモデルをクエリする場合、「削除された」モデルは含まれません...

于 2014-08-29T20:52:09.797 に答える