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.
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.
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With