Laravel 8.x Soft Delete Example Tutorial

In this tutorial i am going to discuss about soft delete in laravel 8. I Will also show you how can we restore our deleted data and can again show it in our laravel application using laravel soft delete. 

When models are soft deleted, they are not actually removed from your database. Instead, a deleted_at attribute is set on the model and inserted into the database. If a model has a non-null deleted_at value, the model has been soft deleted.

In this tutorial i am using User model to our laravel soft delete example. To enable soft deletes for a model, use the Illuminate\Database\Eloquent\SoftDeletes trait on the model: 

app/User.php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable
{
    use Notifiable,SoftDeletes;
}

 

So, how to use in our project, so first when you create table moigration then you have to add softDeletes(). you can see like bellow example of migration.

database/migrations/users.php

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email',191)->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->softDeletes();
            $table->rememberToken();
            $table->timestamps();
    });
}

 

Remember : The SoftDeletes trait will automatically cast the deleted_at attribute to a DateTime / Carbon instance for you.

Querying Soft Deleted Model : 

As noted above, soft deleted models will automatically be excluded from query results. However, you may force soft deleted models to appear in a result set using the withTrashed method on the query:

Now to check it works or not, just open command terminal and run this below command one after another.

php artisan tinker

factory(\App\User::class,1)->create()

 

After running this command, you will see one data is inserted into users table. See the below image.

laravel-soft-delete-example

 

Now time to check soft delete in laravel 6, so retrieve this following data and delete it like below image using following command

$user = \App\User::find(1)

$user->delete()

 

Then you will see our data is deleted. See the below image

soft-delete-example-laravel

 

Now have a look, our data is deleted now. if we wanna retrieve it like below query, it doesn't work. see the example

laravel-soft-delete

 

look, this query didn't find our deleted data. So time to make query with withTrashed. So let's have a try

with-trashed-soft-delete-laravel

 

Boom, our deleted data is here. So we can get back our deleted data using withTrashed(). but do you, we can aslo restore it and can back it with normal eloquent query. Just we have to restore it for doing it.

restore-laravel-soft-delete 

 

Look after restore it, we can retrieve this data with norlam eloquent query, we don't need to use withTrashed() to back it. But if we forceDelete it, then we never back it. 

force-delete-laravel-soft-delete-example

 

Look after force delete it, now there is no data with withTrashed query also. hope you will understand all those thing. Now check all the command and query with one single file.

php artisan tinker

factory(\App\User::class,1)->create()

Illuminate\Database\Eloquent\Collection {
     all: [
       App\User {
         name: "Zachariah Thiel DDS",
         email: "corrine05@example.net",
         email_verified_at: "2020-01-31 10:45:13",
         updated_at: "2020-01-31 10:45:13",
         created_at: "2020-01-31 10:45:13",
         id: 1,
       },
     ],
   }

$user = \App\User::find(1)

App\User {

     id: 1,
     name: "Zachariah Thiel DDS",
     email: "corrine05@example.net",
     email_verified_at: "2020-01-31 10:45:13",
     deleted_at: null,
     created_at: "2020-01-31 10:45:13",
     updated_at: "2020-01-31 10:45:13",
   }

$user->delete()
true

$user = \App\User::find(1)
null

$user = \App\User::where('id',1)->get()
Illuminate\Database\Eloquent\Collection {
     all: [],
   }
$user = \App\User::where('id',1)->first()
null

$user = \App\User::where('id',1)->withTrashed()->first()
 App\User {
     id: 1,
     name: "Zachariah Thiel DDS",
     email: "corrine05@example.net",
     email_verified_at: "2020-01-31 10:45:13",
     deleted_at: "2020-01-31 10:46:38",
     created_at: "2020-01-31 10:45:13",
     updated_at: "2020-01-31 10:46:38",
   }

$user->restore()
true

$user = \App\User::find(1)
App\User {
     name: "Zachariah Thiel DDS",
     email: "corrine05@example.net",
     email_verified_at: "2020-01-31 10:45:13",
     deleted_at: null,
     created_at: "2020-01-31 10:45:13",
     updated_at: "2020-01-31 10:50:52",
   }

$user->forceDelete()
true
$user = \App\User::where('id',1)->withTrashed()->first()
null

 

Read also : Understanding Constructor and Method Dependency Injection in Laravel

 

if you like this tutorial, please share with your friends. Hope it can help you. 

 

#laravel-6 #laravel #laravel-eloquent #soft-delete