数据软删除

先参考Laravel文档实现模型的软删除:

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. class Post extends Model
  6. {
  7. use SoftDeletes;
  8. }

这样在grid列表中显示的数据都是未被删除的数据

  1. return Grid::make(new Post(), function (Grid $grid) {
  2. $grid->id('ID')->sortable();
  3. $grid->title('Title');
  4. $grid->created_at('Created at');
  5. $grid->updated_at('Updated at');
  6. });

回收站入口

接下来需要增加一个入口,能让我们看到被软删除的数据,这里可以使用model-grid范围过滤器来实现

  1. $grid->filter(function () {
  2. // 范围过滤器,调用模型的`onlyTrashed`方法,查询出被软删除的数据。
  3. $filter->scope('trashed', '回收站')->onlyTrashed();
  4. });

在表头的筛选按钮的下拉菜单中就会出现一个回收站按钮,点击它,就会调用模型的onlyTrashed方法,从表中查询出被删除的数据,也就是回收站中的数据。

数据软删除 - 图1

行恢复操作

按照下面的方法,我们可以在回收站中的每一行数据加上一个恢复操作,方便恢复数据

先定义操作类app/Admin/Actions/Post/Restore.php

  1. <?php
  2. namespace App\Admin\Actions\Post;
  3. use Dcat\Admin\Grid\RowAction;
  4. use Illuminate\Http\Request;
  5. class Restore extends RowAction
  6. {
  7. protected $title = '恢复';
  8. protected $model;
  9. // 注意构造方法的参数必须要有默认值
  10. public function __construct(string $model = null)
  11. {
  12. $this->model = $model;
  13. }
  14. public function handle(Request $request)
  15. {
  16. $key = $this->getKey();
  17. $model = $request->get('model');
  18. $model::withTrashed()->findOrFail($key)->restore();
  19. return $this->response()->success('已恢复')->refresh();
  20. }
  21. public function confirm()
  22. {
  23. return ['确定恢复吗?'];
  24. }
  25. public function parameters()
  26. {
  27. return [
  28. 'model' => $this->model,
  29. ];
  30. }
  31. }

添加到行操作:

  1. use App\Models\Post;
  2. use App\Admin\Actions\Post\Restore;
  3. $grid->actions(function (Grid\Displayers\Actions $actions) {
  4. if (request('_scope_') == 'trashed') {
  5. $actions->append(new Restore(Post::class));
  6. }
  7. });

批量恢复操作

先定义操作类app/Admin/Actions/Post/BatchRestore.php

  1. <?php
  2. namespace App\Admin\Actions\Post;
  3. use Dcat\Admin\Grid\BatchAction;
  4. use Illuminate\Http\Request;
  5. class BatchRestore extends BatchAction
  6. {
  7. protected $title = '恢复';
  8. protected $model;
  9. // 注意构造方法的参数必须要有默认值
  10. public function __construct(string $model = null)
  11. {
  12. $this->model = $model;
  13. }
  14. public function handle(Request $request)
  15. {
  16. $model = $request->get('model');
  17. foreach ((array) $this->getKey() as $key) {
  18. $model::withTrashed()->findOrFail($key)->restore();
  19. }
  20. return $this->response()->success('已恢复')->refresh();
  21. }
  22. public function confirm()
  23. {
  24. return ['确定恢复吗?'];
  25. }
  26. public function parameters()
  27. {
  28. return [
  29. 'model' => $this->model,
  30. ];
  31. }
  32. }

添加到批量操作:

  1. use App\Models\Post;
  2. use App\Admin\Actions\Post\BatchRestore;
  3. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  4. if (request('_scope_') == 'trashed') {
  5. $batch->add(new BatchRestore(Post::class));
  6. }
  7. });