How to create a Trash/Deleted section in Backpack CRUD

Are you using SoftDeletes on any of your Eloquent models? Do you want the admin to be able to soft delete, hard delete or recover items...

Karan Datwani
Karan Datwani
Share:

Are you using SoftDeletes on any of your Eloquent models? Do you want the admin to be able to soft delete, hard delete or recover items?

You're in luck then. We have an operation that does just that in Backpack/PRO. But we don't call it SoftDelete... for it to be intuitive to the admin, we call it the TrashOperation. The admin can:

  • trash items
  • recover items from the trash
  • permanently delete the items in the trash

image

Sounds similar to SoftDelete with a UI? That's because it is. It is EXACTLY that. And it's spectacularly easy to use - you just replace your DeleteOperation with the TrashOperation and BOOM - you admin can do all of the above.

How To Setup Trash Operation

First of all, the requirements:

  • You need Backpack/PRO installed;
  • Ensure your model uses Laravel's SoftDeletes trait;

Now, just two steps and you are done:

  1. Use Laravel's SoftDeletes trait in your Model:
use Illuminate\Database\Eloquent\SoftDeletes;

class Invoice extends Model
{
    use SoftDeletes;
}
  1. Replace your DeleteOperation with TrashOperation in your CrudController:
class InvoiceCrudController extends CrudController
{
-    use \Backpack\CRUD\Http\Controllers\Operations\DeleteOperation;
+    use \Backpack\Pro\Http\Controllers\Operations\TrashOperation;
}

Note that the TrashOperation also comes with optional configurations to better fit your requirments - visit the docs to explore available configurations.

And that's it, you're done.

But there are a few more things you might want to do with the TrashOperation, for more complex projects:

Control Access to Operation Actions

When using TrashOperation, each action inside this operation (trash, restore, and destroy) checks for access before being performed. This allows you to control access based on user roles or other criteria:

// If the user is not a super admin, don't allow permanently deleting items
public function setupTrashOperation()
{
    if (!backpack_user()->hasRole('superadmin')) {
         CRUD::denyAccess('destroy');
    }
}

Trash Multiple Items using BulkTrashOperation

In addition to the button for each entry, developers can show checkboxes next to each element, allowing administrators to trash, restore, and delete multiple entries at once.

  1. Ensure your model uses SoftDeletes.
  2. Add \Backpack\Pro\Http\Controllers\Operations\BulkTrashOperation; to your EntityCrudController.

Thanks for reading this far and purchasing Backpack Pro! We will continue coming up with more features.

By following these steps, you seamlessly integrate a Trash/Deleted section into your Backpack CRUD. Allowing admins to trash items, restore them, or delete them permanently.

Happy Coding!

Want to receive more articles like this?

Subscribe to our "Article Digest". We'll send you a list of the new articles, every week, month or quarter - your choice.

Reactions & Comments

What do you think about this?

Cheers @Karan for another quality blog post! One thing I've just noticed is that the "Show" button for the soft-deleted items results in a 404 as the default db scope hides soft-deleted items, so now I'm just working out how to just show them in the show function... Cheers

Latest Articles

Wondering what our community has been up to?