目录
▸
入门
简介
安装
1.x升级指南
版本升级
快速开始
开发前必读
主题与颜色
静态资源
视图与自定义页面
多应用(多后台)
JS组件
常见问题
Laravel Octane
▸
数据表格
基本使用
列的使用和扩展
列的显示和扩展
行的使用和扩展
工具栏
树状表格
组合表头
数据来源以及查询条件
关联关系
查询过滤
列过滤器
快捷搜索
规格筛选器
数据导出
快捷创建
行内编辑
事件
字段翻译
数据软删除
头部和脚部
表格异步渲染
▸
数据表单
基本使用
图片/文件上传
字段的使用
字段扩展
数据源
表单弹窗
关联关系
JSON表单
分步表单
表单验证
工具表单
事件以及表单响应
初始化
表单布局
字段翻译
字段动态显示
▸
数据详情
基本使用
字段显示
关联关系
字段显示扩展
初始化
字段翻译
▸
模型树
基本使用
▸
数据仓库
基本使用
▸
动作
基本使用
数据表格
数据表单
数据详情
模型树
▸
多语言
基本使用
▸
开发扩展
扩展基本使用
开发扩展
▸
页面组件
异步加载
图表
数据统计卡片
模态窗(Modal)
工具表单
下拉菜单
单/复选框
选项卡
警告框
提示窗
Markdown
卡片
▸
区块
区块基本使用
▸
动作以及表单响应
动作以及表单响应
▸
权限控制
权限控制
▸
菜单
菜单基本使用
▸
帮助函数
帮助函数
▸
开发工具
基本使用
▸
自定义登陆认证
自定义登录
▸
自定义头部导航
自定义头部导航条
▸
更新日志
BETA版本更新日志
更新日志
内容导航
1.x
2.x
数据软删除
创建时间:2024-06-26 11:07:18 / 更新时间:2024-06-26 11:07:18
# 数据软删除 先参考`Laravel`文档实现模型的[软删除](https://learnku.com/docs/laravel/6.x/eloquent/5176#soft-deleting): ```php <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Post extends Model { use SoftDeletes; } ``` 这样在`grid`列表中显示的数据都是未被删除的数据 ```php return Grid::make(new Post(), function (Grid $grid) { $grid->id('ID')->sortable(); $grid->title('Title'); $grid->created_at('Created at'); $grid->updated_at('Updated at'); }); ``` ## 回收站入口 接下来需要增加一个入口,能让我们看到被软删除的数据,这里可以使用`model-grid`的[范围过滤器](https://learnku.com/docs/dcat-admin/1.x/query-filtering/8097#scope)来实现 ```php $grid->filter(function ($filter) { // 范围过滤器,调用模型的`onlyTrashed`方法,查询出被软删除的数据。 $filter->scope('trashed', '回收站')->onlyTrashed(); }); ``` 在表头的筛选按钮的下拉菜单中就会出现一个`回收站`按钮,点击它,就会调用模型的`onlyTrashed`方法,从表中查询出被删除的数据,也就是回收站中的数据。 # 数据软删除 先参考`Laravel`文档实现模型的[软删除](https://learnku.com/docs/laravel/6.x/eloquent/5176#soft-deleting): ```php <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Post extends Model { use SoftDeletes; } ``` 这样在`grid`列表中显示的数据都是未被删除的数据 ```php return Grid::make(new Post(), function (Grid $grid) { $grid->id('ID')->sortable(); $grid->title('Title'); $grid->created_at('Created at'); $grid->updated_at('Updated at'); }); ``` ## 回收站入口 接下来需要增加一个入口,能让我们看到被软删除的数据,这里可以使用`model-grid`的[范围过滤器](https://learnku.com/docs/dcat-admin/1.x/query-filtering/8097#scope)来实现 ```php $grid->filter(function ($filter) { // 范围过滤器,调用模型的`onlyTrashed`方法,查询出被软删除的数据。 $filter->scope('trashed', '回收站')->onlyTrashed(); }); ``` 在表头的筛选按钮的下拉菜单中就会出现一个`回收站`按钮,点击它,就会调用模型的`onlyTrashed`方法,从表中查询出被删除的数据,也就是回收站中的数据。 <img style="box-shadow:0 1px 6px 1px rgba(0, 0, 0, 0.12)" width="40%" src="https://cdn.learnku.com/uploads/images/202205/19/98106/7imWhgPa38.png!large"> ## 行恢复操作 按照下面的方法,我们可以在回收站中的每一行数据加上一个恢复操作,方便恢复数据 先定义操作类`app/Admin/Actions/Post/Restore.php`: ```php <?php namespace App\Admin\Actions\Post; use Dcat\Admin\Grid\RowAction; use Illuminate\Http\Request; class Restore extends RowAction { protected $title = '恢复'; protected $model; // 注意构造方法的参数必须要有默认值 public function __construct(string $model = null) { $this->model = $model; } public function handle(Request $request) { $key = $this->getKey(); $model = $request->get('model'); $model::withTrashed()->findOrFail($key)->restore(); return $this->response()->success('已恢复')->refresh(); } public function confirm() { return ['确定恢复吗?']; } public function parameters() { return [ 'model' => $this->model, ]; } } ``` 添加到行操作: ```php use App\Models\Post; use App\Admin\Actions\Post\Restore; $grid->actions(function (Grid\Displayers\Actions $actions) { if (request('_scope_') == 'trashed') { $actions->append(new Restore(Post::class)); } }); ``` ## 批量恢复操作 先定义操作类`app/Admin/Actions/Post/BatchRestore.php`: ```php <?php namespace App\Admin\Actions\Post; use Dcat\Admin\Grid\BatchAction; use Illuminate\Http\Request; class BatchRestore extends BatchAction { protected $title = '恢复'; protected $model; // 注意构造方法的参数必须要有默认值 public function __construct(string $model = null) { $this->model = $model; } public function handle(Request $request) { $model = $request->get('model'); foreach ((array) $this->getKey() as $key) { $model::withTrashed()->findOrFail($key)->restore(); } return $this->response()->success('已恢复')->refresh(); } public function confirm() { return ['确定恢复吗?']; } public function parameters() { return [ 'model' => $this->model, ]; } } ``` 添加到批量操作: ```php use App\Models\Post; use App\Admin\Actions\Post\BatchRestore; $grid->batchActions(function (Grid\Tools\BatchActions $batch) { if (request('_scope_') == 'trashed') { $batch->add(new BatchRestore(Post::class)); } }); ```  ## 行恢复操作 按照下面的方法,我们可以在回收站中的每一行数据加上一个恢复操作,方便恢复数据 先定义操作类`app/Admin/Actions/Post/Restore.php`: ```php <?php namespace App\Admin\Actions\Post; use Dcat\Admin\Grid\RowAction; use Illuminate\Http\Request; class Restore extends RowAction { protected $title = '恢复'; protected $model; // 注意构造方法的参数必须要有默认值 public function __construct(string $model = null) { $this->model = $model; } public function handle(Request $request) { $key = $this->getKey(); $model = $request->get('model'); $model::withTrashed()->findOrFail($key)->restore(); return $this->response()->success('已恢复')->refresh(); } public function confirm() { return ['确定恢复吗?']; } public function parameters() { return [ 'model' => $this->model, ]; } } ``` 添加到行操作: ```php use App\Models\Post; use App\Admin\Actions\Post\Restore; $grid->actions(function (Grid\Displayers\Actions $actions) { if (request('_scope_') == 'trashed') { $actions->append(new Restore(Post::class)); } }); ``` ## 批量恢复操作 先定义操作类`app/Admin/Actions/Post/BatchRestore.php`: ```php <?php namespace App\Admin\Actions\Post; use Dcat\Admin\Grid\BatchAction; use Illuminate\Http\Request; class BatchRestore extends BatchAction { protected $title = '恢复'; protected $model; // 注意构造方法的参数必须要有默认值 public function __construct(string $model = null) { $this->model = $model; } public function handle(Request $request) { $model = $request->get('model'); foreach ((array) $this->getKey() as $key) { $model::withTrashed()->findOrFail($key)->restore(); } return $this->response()->success('已恢复')->refresh(); } public function confirm() { return ['确定恢复吗?']; } public function parameters() { return [ 'model' => $this->model, ]; } } ``` 添加到批量操作: ```php use App\Models\Post; use App\Admin\Actions\Post\BatchRestore; $grid->batchActions(function (Grid\Tools\BatchActions $batch) { if (request('_scope_') == 'trashed') { $batch->add(new BatchRestore(Post::class)); } }); ```
上一文章
下一文章
返回顶部
返回主页
返回文档展示页