乌鸦嘴文档 乌鸦嘴文档
乌鸦嘴社区 (opens new window)
乌鸦嘴社区 (opens new window)
  • 入门

  • 数据列表

    • 表格基本使用
    • 列的基本使用
    • 列的显示和扩展
    • 行的使用和扩展
    • 工具栏
    • 树状表格
    • 组合表头
    • 表格数据源
    • 表格关联关系
    • 查询过滤
    • 列过滤器
    • 表格快捷搜索
    • 表格规格筛选器
    • 数据导出
    • 快捷创建
    • 数据表格行内编辑
    • 数据表格事件
    • 表格字段翻译
    • 数据软删除
      • 回收站入口
      • 行恢复操作
      • 批量恢复操作
    • 头部和脚部
    • 表格异步渲染
  • 数据表单

  • 数据详情

  • 模型树

  • 数据仓库

  • 动作

  • 多语言

  • 开发扩展

  • 页面组件

  • 区块

  • 动作以及表单相应

  • 权限控制

  • 菜单

  • 帮助函数

  • 开发工具

  • 自定义登陆

  • 自定义头部导航

  • 更新日志

目录

数据软删除

# 数据软删除

先参考Laravel文档实现模型的软删除 (opens new window):

<?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 () {

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

});

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

# 行恢复操作

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

先定义操作类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));
	}
});
表格字段翻译
头部和脚部

← 表格字段翻译 头部和脚部→

Theme by Vdoing | Copyright © 2020-2022 wyz.xyz 宁ICP备15001739号-5
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×