How to Use Soft Delete in Laravel

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

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?