Laravel Add Soft Delete to Existing Table Example
Hi Developer,
This definitive guide, we will show you laravel add soft delete to existing table. I explained simply about how to add soft delete in existing table laravel. It's a simple example of laravel soft delete in existing table. I’m going to show you about laravel add soft delete to existing table.
If you want to add soft delete in the existing table with laravel then i will show you the following two things you need to do.
1. Create Migration and add deleted_at column using softDeletes() function.
2. Use Illuminate\Database\Eloquent\SoftDeletes facade in model and use SoftDeletes class.
So, let's see the following step to adding soft delete to an existing table in laravel.
Step 1: Add deleted_at Column using Migration:
let's create new migration using following command:
php artisan make:migration add_soft_delete_posts
next, updated migration file as like the below:
database/migrations/2023_01_16_134448_add_soft_delete_posts.php
<?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::table('posts', function(Blueprint $table)
{
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('posts', function(Blueprint $table)
{
$table->dropSoftDeletes();
});
}
};
Now, you can run migration:
php artisan migrate
Step 2: Use Soft Delete in Model
we need to use Illuminate\Database\Eloquent\SoftDeletes facade in model and use SoftDeletes class.
let's update Post.php model class.
app/Models/Post.php
<?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'
];
}
Step 3: Delete Record
You can remove record as like below query code:
$post = Post::find(1);
$post->delete();
Output:
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Migration Enum Default Value Example
- How to Rollback Migration in Laravel?
- Laravel Migration Add Comment to Column Example
- How to add Default Value of Column in Laravel Migration?
- Laravel Migration Custom Index Name Example
- Laravel Migration Custom Foreign Key Name Example
- How to Add Index in Laravel Migration?
- How to Add Foreign Key in Laravel Migration?
- Laravel Migration - How to Add New Column in Existing Table ?
- How to Change Table Name using Laravel Migration?
- How to Remove Column from Table in Laravel Migration?
- How to Change Column Name and Data Type in Laravel Migration?
- How to Create Table using Migration in Laravel?