Once the model, migration, and factory are generated, open the migration file from the database/migrations folder and update with the below one. The -mf flag will create a migration and factory for the Post model. Open the terminal and run the below command. Lets say you have Folder and File Eloquent models that are related and use SoftDeletes trait and when you delete a folder you also want to delete files in. For this example I will be creating a Post model.
The first thing we will do in this newly created application, create a model and migration. Now update the database credentials in the. Once the application installation finish, create a new database in your phpMyAdmin or any MySQL client you are using. composer create-project laravel/laravel SoftDeleteApp Before getting started, be sure to configure a database connection in your application's config/database.php configuration file. so we can easily delete data into the database using the Delete Query eloquent method. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well. Open your command line terminal and run the below command. The laravel eloquent provides many types of eloquent methods. Creating Laravel Applicationįor the purpose of some practical examples, let’s create a new Laravel application using composer. So any model has a non-null deleted_at value will be considered as soft deleted model. To do the same thing with Eloquent, we could use the DB facade as follows: DB::table('posts')->truncate() Alternatively, you could call the truncate() method directly on your model: Post::truncate() Delete all entries using the delete() method. When you will look into the database table. Instead, a deleted_at attribute is set on the model and inserted into the database. WikipediaĪbove definition is simple enough to understand that when models are soft deleted, they are not actually removed from your database.
Before diving into this topic, let me explain what the heck is soft delete.Īn operation in which a flag is used to mark data as unusable, without erasing the data itself from the database. The syntax of delete method is shown in the following table. Today, we will be exploring how we can practically use the soft delete functionality. Laravel - Delete Records, We can delete the record using the DB facade with the delete method. Laravel provides amazing features to deal with your database records and soft delete is one of them.