How to use Laravel Soft Deletes
Tags: laravel, php, webdev
Published: 2022-08-31 23:45:35

The Laravel web framework has been considered the best PHP framework for website development for many years. It provides ready-made template solutions for the implementation of web projects of varying complexity.

**What is Laravel?**

Prior to going into Laravel’s unique Soft Delete feature, let’s understand what Laravel encompasses. Laravel is an MVC framework for fast, convenient and, most importantly, correct writing of sites in PHP. The framework has a large set of features, plugins and templates that allow you to bring even the most ambitious projects to life.

The Laravel web framework is open source and absolutely free for all developers. The project source code can be found on the GitHub page.

The framework often receives various awards and accolades. For example, Laravel gets the title of the best PHP framework for both corporate and personal use and the popularity of the framework is constantly growing.

**Why should we use Laravel?**

Laravel values beauty and it is worth using if only for the reason that it allows you to write functional, clean and attractive code. After all, if you write code yourself, there is a possibility that you can implement something not so simple and elegant, but through this framework everything turns out beautifully and correctly.

Laravel allows you to do things like: unit testing, tracking URLs, setting security, working with sessions and creating an authorisation system, light database work, working with mail, error tracking and many other things. All this is possible to implement without Laravel, but in implementing it you will use ready-made solutions and your code will turn out to be much simpler and smaller than writing everything yourself.

**Laravel Soft Deletes**

Even if you provide users several warnings before they permanently remove a record from your database, it still happens that people delete data by accident in your applications. What you can do to backup data, is introduce the idea of a trash can that your customers are familiar with from their desktop and this way you can control when the trashed data is erased for good. 

If the scenario above is relevant to you, then the Soft Deletes function of Laravel is what you're looking for. All you have to do is add a new database column to your Laravel Model and use the ‘SoftDeletes’ trait on your model. This will allow you to restore deleted data easily and quickly when a user mistakenly deletes some data.

A soft deleting feature is provided by Laravel using the Illuminate\Database\Eloquent\SoftDeletes trait.

**Code examples**

**force delete soft delete Laravel**

Soft Delete : $user->delete();
Force Delete : $user->forceDelete();
Restore Soft Deleted Item : $user->restore(); 


**soft delete Laravel**

/** in migrations this changes need to
    add for table we want to add soft delete (LARAVEL)*/

	/** The migrations. START */
	public function up()
	{
		Schema::table('users', function(Blueprint $table)
		{
			$table->softDeletes();
		});
	}
	/** The migrations. END */

	/** after adding softdelete you need to
    point that column in table related model (LARAVEL)*/

	/** The Model. START */
  	use Illuminate\Database\Eloquent\SoftDeletes;
  	class User extends Model {
	  use SoftDeletes;
	  protected $dates = ['deleted_at'];
	}
	/** The Model. END */


**laravel restore soft delete**

Post::withTrashed()->find($post_id)->restore();

**Conclusion**
Every company is unique. When developing applications, each business develops a unique list of their needs, software requirements, and indicators to track performance. Whether you're upgrading an existing application or building a new one from scratch, always start with the application's performance requirements, and also consider why you're specifying those requirements. Answers these questions are needed to make decisions about the architecture of the application. If possible, you should also consider on-premises and hybrid deployments. This will ensure that your choice is compatible with the languages ​​and frameworks you use. Laravel is the ideal PHP framework for business-related projects.