Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The following soft delete code works fine for me:

$post = Post::find($post_id);
$post->delete();

The deleted_at field is updated. But this gives me an error:

$post = Post::find($post_id);
$post->restore();

Here's the error:

exception 'SymfonyComponentDebugExceptionFatalErrorException' with message 'Call to a member function restore() on a non-object'

I'm stumped. Google is no help so far.

question from:https://stackoverflow.com/questions/25576024/laravel-soft-delete-restore-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
837 views
Welcome To Ask or Share your Answers For Others

1 Answer

Error says $post is a non-object, Laravel doesn't return trashed records without 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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...