Soft delete is a feature in Laravel that allows you to delete records from the database without actually deleting them. Instead, the deleted records are marked as deleted and are hidden from the query results.
To use soft delete in Laravel, you need to add the SoftDeletes trait to your model. This trait provides the necessary methods to mark a record as deleted and to retrieve only the non-deleted records.
Once the SoftDeletes trait is added to your model, you can use the delete() method to mark a record as deleted. This method will set the deleted_at column to the current timestamp.
You can also use the restore() method to restore a deleted record. This method will set the deleted_at column to null.
To retrieve only the non-deleted records, you can use the withTrashed() method. This method will return all records, including the deleted ones.
To retrieve only the deleted records, you can use the onlyTrashed() method. This method will return only the deleted records.
Finally, you can use the forceDelete() method to permanently delete a record from the database. This method will delete the record from the database and will not set the deleted_at column.
How to Use Soft Delete in Laravel