To restore or permanently delete a soft-deleted record using Laravel's Query Builder, you can directly interact with the database using SQL queries. Here's how you can achieve both actions:
Restoring a Soft Deleted Record
To restore a soft-deleted record, you need to set the deleted_at column to NULL. Here's an example using Query Builder:
use Illuminate\Support\Facades\DB;
DB::table('your_table_name')
->where('id', $recordId) // Replace $recordId with the ID of the record you want to restore
->update(['deleted_at' => null]);
Permanently Deleting a Soft Deleted Record
To permanently delete a soft-deleted record, you can simply delete the record from the database:
use Illuminate\Support\Facades\DB;
DB::table('your_table_name')
->where('id', $recordId) // Replace $recordId with the ID of the record you want to delete
->delete();
Notes
- Replace
your_table_namewith the actual name of your table. - Replace
$recordIdwith the ID of the record you want to restore or delete. - Ensure that your table has a
deleted_atcolumn if you are using soft deletes.
These operations bypass Eloquent's built-in soft delete functionality, so use them with caution to ensure data integrity.