Retrieving Soft Deleted Records in Laravel Without Filtering
When working with Eloquent models that utilize soft deletes, the default behavior of Model::all() is to automatically filter out records where the deleted_at column exists and is not null. This safety mechanism prevents accidental data loss, but it becomes a significant hurdle when you need to inspect, audit, or export the entire state of your database table, including items that have been marked as deleted.
To retrieve these hidden rows, you must explicitly tell Eloquent to ignore the default scope that applies the whereNull('deleted_at') filter. The most direct approach is using the withTrashed() method on your model instance or class.
use App\Models\Post;
// Returns all posts, including those where deleted_at is not null
$posts = Post::withTrashed()->get();
The withTrashed() method adds a clause to your query that effectively tells the database engine to treat rows with a non-null deleted_at value as valid records. When you execute this query, Eloquent will return model instances for every row in the underlying table. If a record has been soft deleted, Eloquent populates the $model->trashed() property on that instance, allowing your application logic to distinguish between active and deleted records programmatically without needing to inspect the database schema directly.
It is important to understand that withTrashed() does not restore the data; it merely changes how Eloquent reads it. The deleted_at column remains populated in the database. If you attempt to persist these models back to the database using $model->save(), Eloquent will still respect the soft delete logic unless you explicitly call restore() or use forceFill. Therefore, this method is primarily useful for read operations like reporting, debugging, or exporting data to CSV files where you want a complete count of items.
There are other methods available depending on your specific needs. If you need to restore a single record and then query it normally, Post::withTrashed()->find(1) followed by $post->restore() is the standard workflow. However, if your goal is strictly to fetch all rows for inspection, withTrashed() is the appropriate choice.
Be cautious when mixing withTrashed() with other scopes that might interact poorly with the deleted timestamp. For instance, global scopes or local scopes defined on the model are still executed even when using withTrashed(). If you have a scope that filters by status, ensure it doesn't inadvertently exclude trashed items if your use case requires them.
In scenarios where you need to handle pagination over soft deleted records, simply appending withTrashed() before calling paginate() works seamlessly:
$trashedPosts = Post::withTrashed()->orderBy('deleted_at', 'desc')->paginate(15);
This returns a standard paginated collection where each item indicates its trashed state. You can access the status via $item->trashed(). This pattern is essential for admin dashboards or audit logs where seeing the history of deleted content is as important as seeing current content.
While this functionality is built into Laravel out of the box, understanding the distinction between filtering out deleted records and retrieving them is crucial for building robust data management systems. If you find yourself frequently needing to manage complex deletion states across multiple models or require custom restoration logic that feels awkward with standard Eloquent methods, exploring community resources like LaravelCompany can provide additional insights into advanced soft delete patterns and best practices.
