# 建表
## 配置表
### 建表语句
~~~SQL
DROP TABLE IF EXISTS `config`;
CREATE TABLE `config`
(
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`title` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '配置名',
`value` varchar(600) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '配置值',
`desc` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '配置描述',
`created_at` timestamp NULL DEFAULT '0000-00-00 00:00:00' COMMENT '添加时间',
`updated_at` timestamp NULL DEFAULT '0000-00-00 00:00:00' COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB
AUTO_INCREMENT = 1992
CHARACTER SET = utf8mb4
COLLATE = utf8mb4_general_ci COMMENT = '配置表'
ROW_FORMAT = Dynamic;
~~~
### 代码生成
找到后台代码生成 `开发工具 -> 代码生成器` 选择 `config` 表 勾选 `创建模型` `创建数据仓库` `创建控制器` `创建翻译文件`
> 后面都可以按照这种方式生成代码
添加路由
`app\Admin\routes.php`
~~~php
...
use App\Admin\Controllers\ConfigController;
...
Route::group([
...
], function (Router $router) {
...
$router->get('/config/setting', [ConfigController::class,'setting']);
});
~~~
### 创建菜单
创建 `网站` 一级菜单 创建 `网站` 菜单的二级菜单 `系统设置` 路径填写 `/setting/setting`
### 创建工具表单
输入指令
~~~bash
> php artisan admin:form Setting
~~~
### 编辑控制器
`app\Admin\Controllers\ConfigController.php`
~~~php
...
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Card;
use App\Admin\Forms\Setting;
...
public function setting(Content $content)
{
return $content->title('系统配置')->body(new Card(new Setting()));
}
~~~
### 修改工具表单
#### 构建表单
这里的工具表单的语法和普通的表单语法一样 [工具表单](https://learnku.com/docs/dcat-admin/2.x/tools-form/8125#183ca1)
`app/Admin/Forms/Setting.php`
~~~php
public function form()
{
$this->text('title', '网站标题')
->required();
$this->text('keyword', '网站关键词')
->required();
$this->text('description', '网站描述')
->required();
$this->image('logo', '网站LOGO')
->autoUpload()
->move('/system/' . date('Ymd'))
->uniqueName()
->required();
$this->textarea('copyright', '版权')
->required();
$this->text('qq', '客服QQ')
->required();
}
~~~
增加默认值
`app/Admin/Forms/Setting.php`
~~~php
...
use App\Models\Config;
...
public function default()
{
$configModel = new Config();
return [
'title' => $configModel->getConfig('title'),
'keyword' => $configModel->getConfig('keyword'),
'description' => $configModel->getConfig('description'),
'logo' => $configModel->getConfig('logo'),
'copyright' => $configModel->getConfig('copyright'),
'qq' => $configModel->getConfig('qq'),
];
}
~~~
config模型增加根据 配置名获取配置值的方法
`app\Models\Config.php`
~~~php
public function getConfig($title)
{
$value = self::where('title', $title)
->firstOr(function () {
return '';
});
return $value->value ?? $value;
}
~~~
添加表单处理代码 `app\Admin\Forms\Setting.php`
~~~php
public function handle(array $input)
{
foreach ($input as $title => $value) {
$configModel = new Config();
$configModel->createOrUpdate($title, $value);
}
return $this
->response()
->success('更新成功')
->refresh();
}
~~~
增加对应的逻辑处理 `app\Models\Config.php`
~~~php
public function createOrUpdate($title = '', $value = '')
{
if ($title) {
$config = self::where('title', $title)
->first();
if ($config) {
if ($config->value !== $value) {
self::where('title', $title)
->update([
'value' => $value,
'updated_at' => date('Y-m-d H:i:s'),
]);
}
} else {
self::where('title', $title)
->insert([
'title' => $title,
'value' => $value,
'created_at' => date('Y-m-d H:i:s'),
]);
}
}
}
~~~
此时上传表单发现 图片可以正常上传但是不显示 是因为路径不对
修改配置文件
`config\filesystems.php`
~~~php
'disks' => [
...
'admin' => [
'driver' => 'local',
'root' => public_path('uploads'),
'visibility' => 'public',
'url' => env('APP_URL').'/uploads',
],
],
~~~
`config\admin.php`
~~~php
'upload' => [
// Disk in `config/filesystem.php`.
'disk' => 'admin',
...
],
~~~
重新上传即可