Dcat Admin
Dcat Admin
是一个基于laravel-admin
二次开发而成的后台系统构建工具,只需极少的代码即可快速构建出一个功能完善的高颜值后台系统。支持页面一键生成CURD
代码,内置丰富的后台常用组件,开箱即用,让开发者告别冗杂的HTML
代码,对后端开发者非常友好。
- Dcat Admin 文档
- 考试系统-管理员后台
- 考试系统-考生后台
各种问题
文件上传下载
- 修改
Config
目录下admin.php
文件1
2
3
4
5
6
7
8
9
10'upload' => [
// Disk in `config/filesystem.php`.
'disk' => 'admin',
// Image and file upload path under the disk above.
'directory' => [
'image' => 'images',
'file' => 'files',
],
], Filesystems.php
文件1
2
3
4
5
6
7
8'disks' => [
'admin' => [
'driver' => 'local',
'root' => public_path('uploads'),
'visibility' => 'public',
'url' => env('APP_URL').'/uploads',
],
],- 修改
.env
文件1
2
3
4
5APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:ApdWBVhTPxwnoM59rhazb4/XiN4OSL7lkMGQDFxmie0=
APP_DEBUG=true //正式环境 禁用开发工具 改成false
APP_URL=http://localhost:8090 //文件不显示 注意端口号
多语言配置
多语言目录文件:D:\GitHub\EUREKA-EMS\resources\lang\zh-CN
安装:composer require "overtrue/laravel-lang:~3.0"
完成上面的操作后,将项目文件 config/app.php
中的下一行Illuminate\Translation\TranslationServiceProvider::class,
替换为:Overtrue\LaravelLang\TranslationServiceProvider::class,
如果你想修改扩展包提供的语言文件,可以使用以下命令发布语言文件到项目里:php artisan lang:publish zh-CN
Pjax刷新页面
1 | Admin::script( |
权限控制
只查询和当前登录用户有关联的数据
1 | $grid->model()->where('que_create_byid',Admin::user()->id); |
管理员用户数据
文件在 vendor\dcat\laravel-admin\src\Controllers\UserController.php
多后台
- 生成新应用
1 | php artisan admin:app NewAdmin |
- 启用
开配置文件config/admin.php
,加入以下代码
1 | return [ |
浏览器访问这个新应用了http://localhost:8000/new-admin
- 更改菜单
执行sql脚本
1 | /* |
Excel导入
前期准备
- 安装
1
composer require maatwebsite/excel
Maatwebsite\Excel\ExcelServiceProvider
在config/app.php
以下位置添加ServiceProvider
:
1 | 'providers' => [ |
- 手动添加,
config/app.php
1
2
3
4'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
] - 发布配置将创建一个新配置文件
1
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
config/excel.php
导入
- 创建一个导入类
app/Imports
1
2
3
4
5
6
7php artisan make:import QuestionImport --model= Question
├── app
│ ├── Imports
│ │ ├── QuestionImport.php
│
└── composer.json
1 |
|
注意:模型需要与QuestionImport 字段名一致
模型 Model
1 |
|
生成工具表单
1
php artisan admin:form QuestionData
修改:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
namespace App\Admin\Forms;
use App\Imports\QuestionImport;
use Dcat\Admin\Widgets\Form;
use Symfony\Component\HttpFoundation\Response;
use Maatwebsite\Excel\Facades\Excel;
class QuestionData extends Form
{
// 增加一个自定义属性保存用户ID
protected $id = 'DATA_IMPORT';
// 构造方法的参数必须设置默认值
public function __construct($id = null)
{
$this->id = $id;
parent::__construct();
}
/**
* Handle the form request.
*
* @param array $input
*
* @return Response
*/
public function handle(array $input)
{
// 下面的代码获取到上传的文件,然后使用`maatwebsite/excel`等包来处理上传你的文件,保存到数据库
$file = $input['file'];
if(!$file){
return $this->error('请先上传文件');
}
// 导入
Excel::import(new QuestionImport(), $file, 'admin');
return $this->success('导入完成!');
}
/**
* Build a form here.
*/
public function form()
{
$this->file('file', '请选择文件')
->disk('admin')
->autoUpload()
->accept('xlsx,xls,csv');
}
}弹窗显示
运行php artisan admin:action
命令,选择选项3
1
2
3
4
5
6
7
8
9php artisan admin:action
Which type of action would you like to make?:
[0] default
[1] grid-batch
[2] grid-row
[3] grid-tool
[4] form-tool
[5] show-tool
[6] tree-tool修改:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
namespace App\Admin\Actions\Grid;
use App\Admin\Forms\QuestionData;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid\Tools\AbstractTool;
class GridQuestionData extends AbstractTool
{
/**
* @return string
*/
protected $title = '导入';
protected function script()
{
$url = request()->fullUrlWithQuery(['gender' => '_gender_']);
return <<<JS
$('input:radio.user-gender').change(function () {
var url = "$url".replace('_gender_', $(this).val());
Dcat.reload(url);
});
JS;
}
public function render()
{
Admin::script($this->script());
$id = "DATA_IMPORT";
$this->modal($id);
return <<<HTML
<span data-toggle="modal" data-target="#{$id}" style="float: right">
<a class="btn btn-primary btn-outline" style="color: #4199de"><i class="feather icon-share"></i> 导入</a>
</span>
HTML;
}
protected function modal($id)
{
// 表单
$form = new QuestionData();
Admin::html(
<<<HTML
<div class="modal fade" id="{$id}">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">导入数据</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
{$form->render()}
</div>
</div>
</div>
</div>
HTML
);
}
}在grid 工具栏 使用
1
2//导入
$grid->tools(new GridQuestionData());
表格
模型树
表结构和模型
使用model-tree,要遵守约定的表结构。表格结构里面有三个必要的字段parent_id、order、title,表结构中的三个字段parent_id、order、title的字段名也是可以修改的(修改为:sort_p_id、sort_name),parent_id字段一定要默认为0
1 | CREATE TABLE `shiyu_sort` ( |
对应的模型
1 | namespace App\Models\Blog; |
页面使用方法
1 | namespace App\Admin\Controllers\Blog; |
表单
下拉框
单选
1
2
3
4$form->select('subject_set')->options(function () {
$roleModel = Major::class;
return $roleModel::all()->pluck('major_name', 'id');
})->required();多选
1
2
3
4
5
6
7
8
9
10$form->multipleSelect('subject_set')
->options(function () {
$roleModel = Major::class;
return $roleModel::all()->pluck('major_name', 'id');
})
//从数据库中查出的二维数组中转化成ID
->customFormat(function ($v) {
return array_column($v, 'id');
});弹窗选择
单选(2.0版本弃用)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15// 方法1
$form->selectResource('questype_id', '题型')->path('/TiKu/Questype')
->multiple(1) // 设置为单选
->options(function ($v) {
if (!$v) return $v;
return Questype::find($v)->pluck('type_name', 'id');
});
// 方法2
$menuModel = "App\Models\Blog\ShiyuSort";
$form->select('sort_p_id')->options(function () use ($menuModel) {
return $menuModel::selectOptions();
})->saving(function ($v) {
return (int)$v;
});多选(2.0版本弃用)
1
2
3
4
5
6$form->selectResource('major_id', '申报专业')->path('/TiKu/Major')
->multiple() // 设置为多选
->options(function ($v) {
if (!$v) return $v;
return Major::find($v)->pluck('major_name', 'id');
});弹窗选择新方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15//单选 (selectTable )
$this->multipleSelectTable('recent_news')->title('弹窗标题')
->dialogWidth('50%') // 弹窗宽度,默认 800px
->from(ShiyuArticleTable::make(['id' => $this->getKey()])) // 设置渲染类实例,并传递自定义参数
->model(ShiyuArticle::class, 'id', 'artcles_title'); // 设置编辑数据显示
//多选 (multipleSelectTable)
$this->multipleSelectTable('recent_news')->title('弹窗标题')
->dialogWidth('50%') // 弹窗宽度,默认 800px
->from(ShiyuArticleTable::make(['id' => $this->getKey()])) // 设置渲染类实例,并传递自定义参数
->model(ShiyuArticle::class, 'id', 'artcles_title') // 设置编辑数据显示
->saving(function ($v) {
// $v 是表单提交的字段值,默认是数组类型,这里需要手动转换一下
// 保存为以 "," 隔开的字符串,如果是多对多关联关系,则不需要转换。
return implode(',', $v);
});定义渲染类如下,需要继承
Dcat\Admin\Grid\LazyRenderable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34namespace App\Admin\Renderable;
use App\Models\Blog\ShiyuArticle;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;
class ShiyuArticleTable extends LazyRenderable
{
public function grid(): Grid
{
// 获取外部传递的参数
$id = $this->id;
return Grid::make(new ShiyuArticle(), function (Grid $grid) {
$grid->column('id')->sortable();
$grid->column('artcles_title','文章标题');
$grid->column('artcles_views','浏览量');
$grid->column('artcles_likes','点赞数');
$grid->column('created_at');
$grid->column('updated_at')->sortable();
// 如果表格数据中带有 “name”、“title”或“username”字段,则可以不用设置
$grid->rowSelector()->titleColumn('artcles_title');
$grid->quickSearch(['id', 'artcles_title']);
$grid->paginate(10);
$grid->disableActions();
// $grid->filter(function (Grid\Filter $filter) {
// $filter->like('artcles_title')->width(4);
// });
});
}
}
json字段
1 | $form->table('exam_set', function ($table) { |
标签 (tags)
插入逗号(,)隔开的字符串tags,注意:处理ManyToMany
关系时必须调用pluck
方法,指定显示的字段名和主键。 此外 options
方法传入一个Collection
对象时,options
会自动调用该对象的pluck
方法转为[‘主键名’ => ‘显示字段名’] 数组,作为下拉框选项。或者可以直接使用[‘主键名’ => ‘显示字段名’]这样的数组作为参数。
格式化待渲染的数据 (customFormat
),通过customFormat
方法把从数据库查出的字段值转化为array
格式。
1 | $form->tags('artcles_label') |
自定义页面
首先控制器代码:
1 | namespace App\Admin\Controllers\Shiyu; |
Form表单
1 | namespace App\Admin\Forms; |
根据前台json数组,动态建表
做的时候,$data
总是传不到Schema::create('stmt_plan_list', function ($table){...}
里,自己错误写法是这样的:
1 | Schema::create('stmt_plan_list', function ($table,$data) { |
正确写法应该是用 use ($data)
:
1 | ->saved(function (Form $form, $result) { |