{"id":1148,"date":"2023-10-11T14:20:25","date_gmt":"2023-10-11T14:20:25","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=1148"},"modified":"2023-10-26T14:45:50","modified_gmt":"2023-10-26T14:45:50","slug":"how-to-use-soft-deleting-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/how-to-use-soft-deleting-in-laravel\/","title":{"rendered":"How to use soft deleting in Laravel"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/10\/image-18.png\" alt=\"\" class=\"wp-image-1150\" width=\"432\" height=\"242\"\/><\/figure>\n\n\n\n<p>Soft deleting is a Laravel feature that allows you to hide records from your application without permanently deleting them from the database. This can be useful for a variety of reasons,<\/p>\n\n\n\n<p> such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keeping a record of deleted records for audit purposes<\/li>\n\n\n\n<li>Giving users the ability to restore deleted records<\/li>\n\n\n\n<li>Avoiding database fragmentation<\/li>\n<\/ul>\n\n\n\n<p>To use soft deleting in Laravel, you need to:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Add a&nbsp;<code>deleted_at<\/code>&nbsp;column to your database table. This column should be of type&nbsp;<code>datetime<\/code>&nbsp;and nullable.<\/li>\n\n\n\n<li>Add the&nbsp;<code>SoftDeletes<\/code>&nbsp;trait to your model.<\/li>\n\n\n\n<li>Call the\u00a0<code>delete()<\/code>\u00a0method on the model to soft delete it.<\/li>\n<\/ol>\n\n\n\n<p>Soft deleting is a crucial feature in Laravel, a popular PHP framework, that allows you to &#8220;delete&#8221; records from a database while preserving their existence in the database. This feature is a powerful tool for protecting your data integrity and complying with legal or regulatory requirements. It is removed from the database permanently. Soft deleting, however, doesn&#8217;t actually erase the data but instead marks it as &#8220;deleted.&#8221; This way, the information remains in the database but is no longer visible in standard queries. Soft deleting is especially useful when you need to maintain a historical record of data changes, comply with data retention policies, or recover accidentally deleted data.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Database Migration<\/h3>\n\n\n\n<p>In your database migration file, add a <code>deleted_at<\/code> column of type <code>timestamp<\/code> to the table you want to enable soft deleting for. This column will store the deletion timestamp for each record.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Schema::create('your_table', function (Blueprint $table) {\r\n    \/\/ ... other columns\r\n    $table->softDeletes(); \/\/ Adds the 'deleted_at' column\r\n});\r\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Model Configuration<\/h3>\n\n\n\n<p>In your model, use the <code>SoftDeletes<\/code> trait provided by Laravel. This trait adds the necessary methods and properties to the model.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Illuminate\\Database\\Eloquent\\Model;\r\nuse Illuminate\\Database\\Eloquent\\SoftDeletes;\r\n\r\nclass YourModel extends Model\r\n{\r\n    use SoftDeletes;\r\n    \/\/ ...\r\n}\r\n<\/code><\/pre>\n\n\n\n<p>That&#8217;s it! Your Laravel model is now set up for soft deleting.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Performing Soft Deletes<\/h2>\n\n\n\n<p>To soft delete a record, use the <code>delete<\/code> method on your model instance. Laravel will automatically set the <code>deleted_at<\/code> column to the current timestamp without physically removing the record from the database.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$record = YourModel::find($id);\r\n$record->delete();\r\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Restoring Soft Deleted Records<\/h2>\n\n\n\n<p>Soft deleted records can be restored easily. Use the <code>restore<\/code> method on a model instance to bring the record back to life.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$record = YourModel::withTrashed()->find($id); \/\/ Find the soft deleted record\r\n$record->restore(); \/\/ Restore the record\r\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Permanently Deleting Records<\/h2>\n\n\n\n<p>In some cases, you may want to remove soft-deleted records permanently from the database. To do this, use the <code>forceDelete<\/code> method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$record = YourModel::withTrashed()->find($id); \/\/ Find the soft deleted record\r\n$record->forceDelete(); \/\/ Permanently delete the record\r\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Querying Soft Deleted Records<\/h2>\n\n\n\n<p>To retrieve soft deleted records alongside active ones, use the <code>withTrashed<\/code> method in your query:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$records = YourModel::withTrashed()->get();\r\n<\/code><\/pre>\n\n\n\n<p>To retrieve only soft deleted records, you can use the <code>onlyTrashed<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$records = YourModel::onlyTrashed()->get();\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Soft deleting is a Laravel feature that allows you to hide records from your application without permanently deleting them from the database. This can be useful for&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[206,448,42,449],"class_list":["post-1148","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-database","tag-how-to-use-soft-deleting-in-laravel","tag-laravel","tag-soft-deleting"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=1148"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1148\/revisions"}],"predecessor-version":[{"id":1151,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1148\/revisions\/1151"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=1148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=1148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=1148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}