Soft Delete & Force Delete Example in Laravel
In this tutorial, we’re going to learn how soft delete works in Laravel. When models are soft deleted, they are not actually removed from your database. Instead, a timestamp is set on the deleted_at column.
Table of Contents
Create Migration & Model
We’ll test soft delete for products table. Let’s create a migration and model for products table:
php artisan make:model Product -m
Go to database/migrations folder and open timestamp_create_products_table.php file. We need to define $table->softDeletes();
line like this:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->float('price');
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
From the app folder, open Product.php file. In the Product model, we have to include use SoftDeletes;
line.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Product extends Model
{
use SoftDeletes;
public $fillable = ['title', 'price'];
}
Insert New Data
For testing purposes, I’ll test insert, delete tasks using Laravel Tinker. Let’s open Laravel Tinker by this command:
php artisan tinker
I’m inserting a new product:
$product = new Product;
$product->title = 'Test Product';
$product->price = 4.32;
$product->save();
We’ve inserted a new product. We can see all inserted products:
$products = Product::get();
Soft Delete
Normal delete will be the soft delete. Let’s delete the product softly:
Product::find($id)->delete();
// or,
Product::destroy($id);
If we soft delete any product, the delete_at column will be updated. To see the deleted data, we can write code like:
$products = Product::onlyTrashed()->get();
If we need to get both deleted and fresh data, there is a way to get both:
$products = Product::withTrashed()->get();
Restore Deleted Data
We can easily restore any deleted data like this:
Product::withTrashed()->find($id)->restore();
Force Delete (Permanently)
If we want to delete permanently, we have this option:
Product::find($id)->forceDelete();
To delete from soft-deleted (trashed) data, we need to write code like:
Product::onlyTrashed()->find(2)->forceDelete();
We can also set conditions at the time of deleting data from the trash. Let’s deleted 30 days of older data from soft deleted data.
Product::onlyTrashed()->where('deleted_at', '<', Carbon::subDays(30))->forceDelete();
The tutorial is over. Thank you. ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)