Home » Easy Laravel Soft Delete Up and Running Tutorial

Easy Laravel Soft Delete Up and Running Tutorial

Loved it? Please Share on Social Media

Introduction to Laravel Soft Delete

We can quickly delete any record or data from our desired database using Laravel. But What if I don’t want to delete my data permanently? In this case, we have to use the Laravel soft delete feature. 

Hello everyone, What’s going on? I hope you are doing fine. In this tutorial, we will learn how to add this feature to our application. It will be a step-by-step tutorial, so you will feel energized doing this. So, let’s start.

Creating Model, Migration & Controller for Laravel Soft Delete

First, You have to set up your fresh Laravel project by downloading it from the GitHub page. Then, you have to create a Model, Migration, & Controller for Post (We’ll use Post for our tutorial purposes). Use the bellow command of your terminal.

php artisan make:model Post -m

after that,

php artisan make:controller PostController

Edit Post Migration File for Laravel Soft Delete

Great, Our first step is complete. Now, we have to edit our post migration file. Please copy and paste the snippets into your create_post_table file.

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('post');
            $table->softDeletes();
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('posts');
    }
};

Adding Fillable Properties & Traits to Post Model

Next, you must add the fillable property & SoftDeletes trait in our Property model, so you must copy and paste the line below into your Property model.

use SoftDeletes;
protected $fillable = ['name', 'post'];

Now, in your terminal, write down the following command to migrate our Property model, and after the command, we will go to our next step.

php artisan migrate

Quick Note:

I’m not showing how to add data on the post table; you will do it on your end. It’s homework for you, haha.

Create Posts Page

Great, Our third step is complete. Now, we have to create a blade file to show our posts. Please create a new file inside the views folder of your application and name it posts.blade.php.

Now, copy and paste the following code snippets to your code editor.

<!doctype html>
<html>
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.rtl.min.css">
    <title>Laravel Soft Delete Tutorial</title>
  </head>
  <body>
    <table class="table">
  <thead>
    <tr>
      <th>ID</th>
      <th>NAME</th>
      <th>POST</th>
      <th>#</th>
    </tr>
  </thead>
  <tbody>
    @foreach($posts as $post)
      <tr>
        <th>{{ $post->id }}</th>
        <th>{{ $post->name }}</th>
        <th>{{ $post->post }}</th>
        <th>
          <a href="{{ url('/delete/post/'.$post->id) }}" class="btn btn-primary">DELETE</a>
        </th>
      </tr>
    @endforeach
  </tbody>
</table>
    <!-- Optional JavaScript; choose one of the two! -->
    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
  </body>
</html>

Tweak the Post Controller For Laravel Soft Delete

Next, open your PostController file and paste the following snippet into your code editor.

public function show_post(){
    $posts = Post::all();
    return view('posts', [
        'posts' => $posts
    ]);
}
public function delete_post($id) {
    $post = Post::find($id);
    $post->delete(); 
}

Configuring Web.php File

Now, open your web.php file, paste the following code, and save it.

Route::get('posts', [PostController::class, 'show_post']);
Route::get('delete/post/{id}', [PostController::class, 'delete_post']);

Great, all our steps are now complete. We will start our local server using our artisan command. php artisan serve. If there is no problem, our project is currently running at http://127.0.0.1:8000/ or http://locahost:8000/. You can checkout documentation for more.

Notes

You can get all your deleted posts using this on your controller. You can return another view for trashed records.

$posts = Post::onlyTrashed()->get();

After That, You can restore your deleted post using bellow code on your application.

$post= Post::withTrashed()->find($id);
$post->restore();

Conclusion

Finished! That’s all about our easy laravel soft delete up and running tutorial. Although I didn’t say more about this topic, it will help you more to add Laravel soft delete to your next project. Thank you so much for reading the whole tutorial from the beginning. If this tutorial would help you a little, remember this post on social media. If you have any queries then Contact Us.


Loved it? Please Share on Social Media

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.