How to Delete Data from Database using Laravel 7 Framework
In Laravel, deleting data from the database can be done easily using Eloquent ORM or the query builder. Below is a guide on how to delete a record using both methods in Laravel 7.
1. Setup Routes
First, define the routes to handle the delete functionality in your routes/web.php
:
-
The route listens for a GET request to
/record/{id}/delete
and calls thedestroy
method in theRecordController
.
2. Create Controller Method for Deleting Data
You will need to create a controller method to handle the deletion of a record. If you don't already have a controller, you can generate one:
Then, in the app/Http/Controllers/RecordController.php
, add the following code to handle the delete operation.
Explanation:
-
findOrFail($id): This method is used to find the record by its ID. If the record does not exist, it will automatically throw a 404 error.
-
$record->delete(): This method deletes the record from the database.
-
After deletion, the user is redirected to the
record.index
route (where you can list all records) with a success message.
3. Create Blade View with Delete Link
Now, create or modify the Blade view where you list the records and provide a link to delete each record. For example, you might have a resources/views/records/index.blade.php
:
Explanation:
-
The Delete link triggers a GET request to
/record/{id}/delete
, which will invoke thedestroy
method in the controller. -
JavaScript
confirm
method: Before proceeding with the deletion, a confirmation dialog is displayed to the user.
4. List Records in the Controller
In your RecordController,
You will also need a method to list all the records so that you can display them in the Blade view.
5. Define the Route for Listing Records
Don't forget to define a route for listing the records in your routes/web.php
:
6. Optional: Soft Delete (Optional)
If you don't want to completely remove the data from the database but rather "soft delete" it (mark it as deleted), you can use Laravel's SoftDeletes feature. This way, records will be marked as deleted without actually removing them from the database.
First, make sure your Record
model uses the SoftDeletes
trait:
Then, add a deleted_at
Column to the table by creating a migration:
Modify the migration to add the deleted_at
column:
Run the migration:
Now, in your RecordController,
You can modify the destroy
method to perform a soft delete instead of a hard delete:
With soft deletes, records are not permanently removed from the database. They are marked with a timestamp in the deleted_at
column. You can still query them, but they won't show up in normal queries unless you include withTrashed()
.
7. Test the Delete Functionality
-
Go to the records list page (
/records
). -
Click the Delete link next to any record.
-
Confirm the deletion in the prompt.
-
The record will be deleted from the database, and you will be redirected back to the records list page with a success message.
Conclusion
-
Delete Data: You learned how to delete data using the
delete()
method in Laravel. -
Soft Delete (Optional): You can choose to soft delete records if you don't want to permanently remove them from the database.
-
Confirm Delete: Added a JavaScript confirmation dialog before deleting records.