# 建表
## 文章表
### 建表语句
~~~SQL
DROP TABLE IF EXISTS `articles`;
CREATE TABLE `articles`
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_id` mediumint(8) UNSIGNED NOT NULL DEFAULT 0 COMMENT '分类ID',
`title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '标题',
`keywords` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '关键词',
`abstract` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '摘要',
`content` longtext CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '内容',
`sort` int(11) NOT NULL DEFAULT 0 COMMENT '排序',
`cover_map` varchar(400) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '封面图',
`is_footer` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否页脚显示 0-否 1-是',
`created_at` timestamp NULL DEFAULT NULL COMMENT '添加时间',
`updated_at` timestamp NULL DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB
AUTO_INCREMENT = 1992
CHARACTER SET = utf8
COLLATE = utf8_general_ci COMMENT = '文章表'
ROW_FORMAT = DYNAMIC;
~~~
### 代码生成
找到后台:`开发工具` -> `代码生成器` 选择 `articles` 表生成代码
系统会自动生成该表对应的增删改查
添加路由: `app\Admin\routes.php`
~~~php
...
Route::group([
...
], function (Router $router) {
...
$router->resource('/articles', 'ArticleController');
});
~~~
### 创建菜单
创建`文章`菜单的二级菜单`文章` 路径填写 `/articles`
### 修改表单代码
`app\Admin\Controllers\ArticleController.php`
~~~php
use App\Models\ArticleCategory;
protected function form()
{
return Form::make(new Article(), function (Form $form) {
$articleCategoryModel = new ArticleCategory();
$form->display('id');
$form->select('category_id', '分类')
->options($articleCategoryModel->selectOptions())
->saving(function ($v) {
return (int)$v;
})
->required();
$form->text('title')
->rules('required')
->required();
$form->text('keywords')
->rules('required')
->required();
$form->text('abstract')
->rules('required')
->required();
$form->editor('content')
->rules('required')
->required();
$form->image('cover_map')
->autoUpload()
->removable(false)
->move('/article/' . date('Ymd'))
->uniqueName()
->saving(function ($v) {
return (string)$v;
});
$form->number('sort');
$form->switch('is_footer', '是否页脚显示')
->default(1);
$form->display('created_at');
$form->display('updated_at');
});
}
~~~
> 本次项目用不到封面图 但是一般文章发布都会有
> 表单开关一般使用 `switch`
### 修改列表代码
`app\Admin\Controllers\ArticleController.php`
~~~php
protected function grid()
{
return Grid::make(new Article(['category']), function (Grid $grid) {
$grid->column('id')
->sortable();
$grid->column('category.title','分类');
$grid->column('title');
$grid->column('keywords');
$grid->column('abstract');
$grid->column('sort');
$grid->column('cover_map')->image('',100,150);
$grid->column('is_footer','是否页脚显示')->switch();
$grid->column('created_at');
$grid->column('updated_at')
->sortable();
$grid->filter(function (Grid\Filter $filter) {
$filter->equal('id');
});
$grid->disableViewButton();
});
}
~~~
> 列表去掉查看按钮的代码是 `$grid->disableViewButton();`
### 添加文章的模型关联
~~~php
public function category()
{
return $this->belongsTo(ArticleCategory::class, 'category_id', 'id');
}
~~~