Spread the love

In this article we will learn to get only soft deleted records in Laravel. In our recent article Use Soft Delete to Temporary (Trash) Delete the Records in Laravel 9 we learnt to delete the file without actually deleting from database and sometimes we want to show only that records which are soft deleted so to fetch the only specif trashed items from database we need to call onlyTrashed method of eloquent model.

onlyTrashed indicates the model that we want to fetch only trashed records. onlyTrashed can be use with conjunction of model and the any first, get and pagainate method as follow


onlyTrashed also worked with relationships for example we want fetch only trashed users along then we can use as below


But make sure you have enable the Soft delete trait in model as below


This trait instruct the eloquent model to exclude the trashed data while querying from the database so we will only get the data which is not deleted means we don’t need to do any extra efforts to tell laravel its deleted this trait Illuminate\Database\Eloquent\SoftDeletes will handle everything and also the onlyTrashed as well .

In this article I am going to create route, one controller, model and migrations file to learn the soft delete functionality.

Let’s start fetch Soft Deleted Records in Laravel 9 with simple step by step

Step 1: Create a fresh laravel project

Open a terminal window and type below command to create a new project

composer create-project --prefer-dist laravel/laravel blog

You can also read this to start with new project

Step 2 : Create a table and model

I assume the you have already installed the laravel and basic connection of it like database connection and composer.


Now, for an example i am creating here a table using migration and then i am going to fill the data in it, so create model and migration

php artisan make:model Article -m

this will generate the model and migration file


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Article extends Model
   use SoftDeletes;
   use HasFactory;

things to note here is we have added Illuminate\Database\Eloquent\SoftDeletes; and then used in class article as below

use SoftDeletes;

and migration file database/migrations/timestamp_create_articles_table.php


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return class extends Migration
     * Run the migrations.
     * @return void
     public function up()
        Schema::create('articles', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()

Make sure you have added $table->softDeletes(); line in your migration to create the deleted_at column in table.


and then migrate the migration

php artisan migrate

Now create seeder in database/seeders/DatabaseSeeder.php


namespace Database\Seeders;

use Illuminate\Database\Seeder;

// Import DB and Faker services
use Illuminate\Support\Facades\DB;
use Faker\Factory as Faker;

class DatabaseSeeder extends Seeder
     * Seed the application's database.
     * @return void
    public function run()
        $faker = Faker::create();

    	for ($i=0;$i<=100;$i++) {
                'title' => $faker->name,
                'body' => $faker->text,
                'email' => $faker->email,
                'updated_at' =>$faker->datetime,
                'created_at' => $faker->datetime              

Run Seeder in command line

php artisan db:seed

Step 3 : Create controller

Let’s create a controller and add a methods showArticle and addArticle

php artisan make:controller ArticleController

and add the below code


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Article;
use Illuminate\Support\Facades\Validator;

class ArticleController extends Controller
    public function showArticle(Request $request)

        $articles = new Article;
        $articles = $articles->when($request->show_trashed=='1',function($q){
         //or we can use directly if don't want it conditionally as below
       // $articles->withTrashed()->get();
        return view('articles.article', ['articles' => $articles]);


    function deleteArticle(Request $request)

        $validator = Validator::make($request->all(), [
            'id' => "required",
        if ($validator->fails()) {
            return redirect()->back()->withInput()->withErrors($validator->errors());
        return back()
            ->with('success', 'Article deleted successfully');

Step 4 : Create View File

Now, show the upload form in view file and also show the list of compressed images


<!DOCTYPE html>

    <title>How to Get Only Soft Deleted Records in Laravel 9 - readerstacks.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

    <div class="container">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h2> How to Get Only Soft Deleted Records in Laravel 9 - readerstacks.com</h2>
            <div class="panel-body">

                @if ($message = Session::get('success'))
                <div class="alert alert-success alert-block">
                    <button type="button" class="close" data-dismiss="alert">×</button>
                    <strong>{{ $message }}</strong>


                <h2>All Articles</h2>
                <a href="{{url('articles?show_trashed=1')}}" class="btn btn-success">Show with Trashed</a>
                <a href="{{url('articles?show_trashed=2')}}" class="btn btn-warning">Show Only Trashed</a>

                <table class="table">


                    @foreach( $articles as $article)
                            <a class='btn btn-danger' href="{{url('delete-article?id='.$article->id)}}">Delete</a>


create another view for show all

Step 4: Create two routes in routes/web.php

One route for show all articles and another route for delete the article.



use App\Http\Controllers\ArticleController;
use Illuminate\Support\Facades\Route;


Results Screenshot:

Leave a Reply