Soft Delete in Laravel 10|9|8|7

Soft Delete in Laravel 10|9|8|7

Laravel soft delete migration example; In this tutorial, you will learn how to use soft delete in laravel 10|9|8|7 using migration with example.

How to Soft Delete in Laravel 10|9|8|7 Migration Example

There are two ways to use soft delete in laravel 10|9|8|7 migration example:

  • Create Migration and add deleted_at column using softDeletes() function
  • Use Illuminate\Database\Eloquent\SoftDeletes facade in model and use SoftDeletes class

Create Migration and add deleted_at column using softDeletes() function

Now, execute the following command on command line or terminal to create migration file:

php artisan make:migration create_posts_table

Then open migration file and update the following code into create_posts_table:

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

After that, execute the following command on terminal to create fields into database table using migration:

php artisan migrate

Use Illuminate\Database\Eloquent\SoftDeletes facade in model and use SoftDeletes class

Now, open post.php model and add Illuminate\Database\Eloquent\SoftDeletes facade in model and use SoftDeletes class; is as follows:

<?php
  
namespace App\Models;
  
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
  
class Post extends Model
{
    use HasFactory, SoftDeletes;
  
    protected $dates = ['deleted_at'];
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    protected $fillable = [
        'title', 'body', 'status'
    ];
}

Conclusion

That’s all; through this tutorial, you have learned how to use softdelete in laravel apps.

Recommended Laravel Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *