Laravel Soft Delete restore() Error
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Laravel's soft delete feature allows developers to temporarily remove data from the database while preserving all details in case they need to be restored later on. The delete() and restore() methods are employed to achieve this functionality. However, some developers might encounter an error when trying to restore a deleted item.
Assume you've encountered the following situation: You have used the method $post = Post::find($post_id); to find a post with a specific ID, and then followed this up by executing the soft delete command using $post->delete();. This successfully removes the post and sets its deleted_at field. Now, you want to restore that post back to its previous state using $post = Post::find($post_id); followed by $post->restore();, but instead, you receive an error stating:
exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function restore() on a non-object'
To resolve this issue, it is essential that you understand how Laravel handles soft delete and how it can impact the object handling process. Let us examine this more closely:
1. When running $post = Post::find($post_id);, a new Eloquent instance of the post model is created, storing all its original details.
2. After executing $post->delete();, the deleted_at field in your database is updated to indicate that the record has been deleted. The Eloquent instance of the post model is no longer a valid object, as it does not have access to its original content due to the soft delete operation.
3. When you run $post = Post::find($post_id);, Laravel retrieves the record from the database based on your specified conditions and reinstates it in the Eloquent instance, allowing you to access the original data once again. However, the newly created object is a fresh instance without its original deleted_at field set; thus, $post->restore(); won't find any restore() method as it was not previously called on the object.
To fix this error, you can follow these steps:
1. Re-execute the original code that led to the soft delete, but instead of deleting the post, reset its deleted_at field to null by running a query like this: DB::table('posts')->where('post_id', $post_id)->update(['deleted_at' => null]);.
2. Re-run your original code that retrieves the post, but make sure you use the updated method call for restoring the record: $post = Post::find($post_id); $post->restore();. This time, it will successfully restore your soft-deleted post and remove the error.
Remember to always check your code and ensure that the correct method is being called on the appropriate object for the desired outcome. Laravel's documentation provides detailed explanations of its features like soft delete, allowing better understanding of how these methods work in practice.
In conclusion, understanding the inner workings of Laravel's soft delete feature and following the guidelines laid out in its documentation can help you effectively resolve errors such as this. By using the correct method calls after running the appropriate queries to update your database records, you will be able to restore a soft-deleted post successfully, without experiencing any issues along the way.