我已经创建了一个带有softdelete
选项的CRUD模块,但是现在我需要将restore
选项添加到列表表
我使用backpackforlaravel
与laravel8 .
frombackpackforlaravel Docs:https://backpackforlaravel.com/docs我激活了软删除:
<?php
namespace AppHttpControllersAdmin;
use BackpackCRUDappHttpControllersCrudController;
class ProductCrudController extends CrudController
{
use BackpackCRUDappHttpControllersOperationsDeleteOperation;
}
我找到这个过滤器来显示删除的清单上的项目:
$this->crud->addFilter([
'type' => 'simple',
'name' => 'trashed',
'label' => 'Trashed'
],
false,
function($values) { // if the filter is active
$this->crud->query = $this->crud->query->onlyTrashed();
});
现在,如何恢复项目?
解决,
如果您正在使用backpackforlaravel
应用以下步骤添加恢复软删除项.
Step1:在Model
上添加这个函数:
这个函数将返回按钮。
public function restoreActionButton()
{
return '<a href="' . url("/admin/product/restore/{$this->id}") . '" class="btn btn-sm btn-link" data-button-type="delete"><i class="la la-trash-restore"></i> Restore </a>';
}
Step2:添加route
到控制器功能:
此路由必须与模型返回的路由匹配
Route::get(
'/admin/product/restore/{id}',
[AppHttpControllersAdminProductCrudController::class, 'restore']
);
Step3:现在恢复功能,将其添加到controller
.
public function restore($id)
{
Product::withTrashed()->find($id)->restore();
return redirect()->back();
}
在
类之上use AppModelsProduct;
Step4:(可选)在相同的controller
上更新过滤器:
这将在用户应用已删除的过滤器时隐藏所有按钮。
$this->crud->addFilter(
[
'type' => 'simple',
'name' => 'trashed',
'label' => 'Trashed'
],
false,
function ($values) { // if the filter is active
// $this->crud->addClause('trashed');
$this->crud->query = $this->crud->query->onlyTrashed();
$this->crud->removeAllButtonsFromStack('line');
$this->crud->addButtonFromModelFunction('line', 'restore', 'restoreActionButton');
}
);