Loading…

What are the difference between soft delete & delete in Laravel?

1. delete()

In case when we used to delete in Laravel then it removed records from the database table.

Example:

$delete = Post::where(‘id’, ‘=’, 1)->delete();

2. softDeletes()

To delete records permanently is not a good thing that’s why laravel used features are called SoftDelete. In this case, records did not remove from the table only delele_at value updated with current date and time.
Firstly we have to add given code in our required model file.

use SoftDeletes;
protected $dates = ['deleted_at'];

After this we can use both cases.

$softDelete = Post::where(‘id’, ‘=’, 1)->delete();

OR

$softDelete = Post::where(‘id’, ‘=’, 1)->softDeletes();

Suggest An Answer

Our Particitants

The difference between delete and soft delete is that delete, will delete the record permantly from the database, while soft delete, will add a column name in the model table, and will be modified when you call the delete function. Soft delete is like moving the record to the recycle bin.
You can use soft delete this way

Soft delete just updates table

While actually removing the records from database tables, Laravel has a feature to soft delete the models. When models are soft deleted, they aren

Soft Deleting

In addition to actually removing records from your database, Eloquent can also

SoftDelete() only updates the cloumn of deleted_at in the database table and Delete function permanently deletes the record from the table.

Latest post from Laravel Interview Questions

Ask Question