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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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