Soft deleted records are getting fetched from database.
I have added soft delete to Category model. ‘deleted_at’ column is being set when I soft delete a record. But, when I fetch the result from database using
Category::latest('id')->paginate(20)
it is returning deleted records as well.
My Category model
class Category extends Model
{
use SoftDeletes;
protected $fillable = [
'name',
'slug',
'description',
'is_active',
'image',
];
}
Check query using telescope and it run this.
select * from `categories` order by `id` desc limit 20 offset 0
But it should run
select * from `categories` where `deleted_at` is null order by `id` desc limit 20 offset 0
2
Answers
if you’re seeing soft deleted records when using
it’s likely because you need to explicitly tell Eloquent to only fetch non-deleted records. By default, latest() and paginate() do not exclude soft deleted records. like below –
You should create a migration as well:
With above, following code will not show deleted categories:
by doing above if its not working, then we have only choice left by using following code: