How to use Soft Delete in Laravel?
In this tutorial, i would like to show you how to use soft delete in laravel. here i will give you example of laravel soft delete. you can use eloquent soft delete in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 project.
i will explain you step by step implementation of soft delete in laravel application. we have to use soft delete for safety and backup in laravel.
How work soft delete, laravel add deleted_at column on the table that be default will be null and when we remove then it will place current timestamp, Laravel Model always fetch that record have only deleted_at = null.
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.
Migration Example:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('items', function(Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('description');
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop("items");
}
}
Ok, now you can find deleted_at column in your items table and you have also model should look like as bellow:
Items Model
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Item extends Model
{
use SoftDeletes;
public $fillable = ['title','description'];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
}
Now, you can use Item model as normally like you use before, you get all record like this way.
$data = Item::get();
It will return all record that have deleted_at = null only and you can also remove record like this way:
$data = Item::find(1)->delete();
You can also get deleted records with soft delete.
$data = Item::withTrashed()->get();
You can check and use it's really good....
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
- How to Add Custom Attribute in Laravel Model?
- Laravel Model Disable Primary Key & Auto Increment Example
- Get Array of Ids from Eloquent Models in Laravel
- How to Set Default Value in Laravel Model?
- Laravel Eloquent WhereNotIn Query Example
- Laravel Eloquent WhereIn Query Example
- How to Create and Use Query Scope in Laravel Eloquent
- How to use Union Query with Laravel Eloquent?
- Laravel Select with Sum Query Example
- Laravel Query Builder Where Exists Example
- Example of unionAll in Query Builder Laravel
- How to Get Query Log in Laravel Eloquent?
- Laravel CKeditor 5 Image Upload Tutorial Example