Sabbir Ahmed

Sabbir Ahmed

1 month ago

Laravel's Soft Delete: Enhancing Data Management

Laravel’s soft delete is a very handy feature to use for data management. We use plenty of data models in moderate or big applications. Most of the time, we use relational databases where models are connected with each other. Hard deleting a record can cause severe problem while querying data on there relational fields. Also sometimes, we might accidentally delete the records from where we might want to undo our choice. But for hard deleting, there is no option to get back the old data. For these cases, soft delete comes very handy. It takes some simple steps to add soft delete feature in Laravel models. 

Add Soft Delete to a Model


To add soft delete in a model, you just have to use SoftDeletes trait in your model like this: 


    
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class YourModel extends Model
{
    use SoftDeletes;
    
    protected $dates = ['deleted_at']; 
}

Lets understand a bit


You will see a column ‘deleted_at’ is added to your table in database. By default it is null. Now if you perform a delete operation, you will notice that ‘deleted_at’ column is filled in with a datetime of deletion. So in real, your record isnt just deleted (I mean hard delete), but these deleted records will not be counted for next queries you try on this model.

Add Soft Delete to Migration


If we want to add soft delete to a table via migration, you can follow this way. 

First create a migration file: 


    
php artisan make:migration add_deleted_at_to_products_table --table=products

Then add $table->softDeletes() in the migration like this: 


    

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddDeletedAtToProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('products', function (Blueprint $table) {
            $table->softDeletes(); // Adding soft delete column
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('products', function (Blueprint $table) {
            $table->dropSoftDeletes(); // Dropping soft delete column
        });
    }
}


Then run the migration 


    
php artisan migrate

You will have to enable softDeletes trait in the model too. 

How would I restore a delete value? 


Now, as I have said before, your deleted records wont be counted when you perform a normal query to this model again. But if you want those to be added in your queries, you can do like this:


    
$product = Product::withTrashed()->find($id);

If you want to restore the deleted record to its normal form, then you can use this code: 


    
$product = Product::withTrashed()->find($id);
$product->restore();

And What If I want to delete permanently?

To delete a record permanently, you can use forceDelete() method: 


    
$product = Product::withTrashed()->find($id);
$product->forceDelete();

Hope you have found this article useful. Let me know your thoughts in the comments or please give a love to this article. 

Wish you a happy day. 


Comments

Login As User
Word Count: 0