Eloquent delete
![eloquent delete eloquent delete](https://cdn.devdojo.com/posts/images/June2021/how-to-count-and-detect-empty-laravel-eloquent-collections3.jpg)
In the second example, we will delete records on the basis of the id. so we can directly pass data without a model. In this step, we load the DB and pass the table name and data into the delete method. so we can easily delete data into the database using the Delete Query eloquent method. In the first example, we are going to use the truncate () function, which is used to delete all the records. The laravel eloquent provides many types of eloquent methods. And one of Eloquent's is not being able to autodelete related objects through SoftDelete when a model has a relationship to another with onDelete ('cascade'). However, sometimes even the very best things have their flaws.
![eloquent delete eloquent delete](https://static.javatpoint.com/tutorial/laravel/images/laravel-eloquent22.png)
Now, when you call the delete method on the model, the deleted_at column will be set to the current date and time. But we can delete it from the cache only if we are using the model of Laravel Eloquent. Eloquent is one of many Laravel features that you just can't deny how cool and useful they are. We can force delete records in our model. There are some way to fetch those records, with use of withTrashed () method. To enable soft deletes for a model, add the Illuminate\Database\Eloquent\SoftDeletes trait to the model and use it by using use keyword like use SoftDeletes This will set deletedat field in database and those records will not come when we fetch records with eloquent. 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.Add deleted_at column in migration using $table->softDeletes().There are only two simple steps to enable soft delete. Also, when you querying the model that uses soft deletes then Laravel retrieves those records which timestamps are nulled in deleted_at column. Instead of that timestamp has been assigned to the deleted_at column. When we use soft delete in the project then records are not actually removed from the database.
![eloquent delete eloquent delete](https://3.bp.blogspot.com/-smakTEvM3fA/VS1Ji6JqBMI/AAAAAAAAAg0/1CoHs91oPi0/s1600/Tux%2B4.jpg)
As the name suggests soft delete is used for deleting record visually that means you can’t see the record on your front-end but those records not actually removed from the database.
#Eloquent delete how to#
In this tutorial, we will learn how to use the Laravel soft delete concept using examples.