Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function restore() on a non-object' 

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

like image 321
sterfry68 Avatar asked Aug 29 '14 20:08

sterfry68


People also ask

How remove soft delete in laravel?

The feature that you are looking for is soft-deleting in Laravel. You can simply add a new database column to your Laravel Model and use the SoftDeletes trait on your model. After that,you are good to go and soft deletes work instantly.

What is soft delete in laravel 8?

Soft deleting the data allows us to easily view and restore the data with minimal work and can be a huge time saver when data is accidentally deleted. Laravel provides support for soft deleting using the Illuminate\Database\Eloquent\SoftDeletes trait.

What is withTrashed in laravel?

Laravel, “withTrashed()” linking a deleted relationship If the user gets deleted, and on the User model we use the SoftDeletes trait, you can use withTrashed() method here.


1 Answers

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

like image 138
im_brian_d Avatar answered Sep 28 '22 10:09

im_brian_d