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...
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:
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.
First of all, the requirements:
Now, just two steps and you are done:
SoftDeletes
trait in your Model:use Illuminate\Database\Eloquent\SoftDeletes;
class Invoice extends Model
{
use SoftDeletes;
}
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:
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');
}
}
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.
SoftDeletes
.\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!
Subscribe to our "Article Digest". We'll send you a list of the new articles, every week, month or quarter - your choice.
What do you think about this?
Wondering what our community has been up to?