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

Categories

0 votes
750 views
in Technique[技术] by (71.8m points)

eloquent - Laravel Soft Delete restore() Error

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

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

1 Answer

0 votes
by (71.8m points)

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...