数据软删除

未匹配的标注

数据软删除

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

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes;
}

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

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范围过滤器来实现

$grid->filter(function ($filter) {

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

});

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

行恢复操作

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

先定义操作类app/Admin/Actions/Post/Restore.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,    
        ];
    }
}

添加到行操作:

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

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,    
        ];
    }
}

添加到批量操作:

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));
    }
});

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
贡献者:2
讨论数量: 0
发起讨论 只看当前版本


暂无话题~