# 建表
## 幻灯片表
### 建表语句
~~~SQL
DROP TABLE IF EXISTS `slides`;
CREATE TABLE `slides` (
`id` smallint(5) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`image` varchar(300) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '图片路径',
`url` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '链接地址',
`sort` smallint(5) UNSIGNED NOT NULL DEFAULT 0 COMMENT '排序',
`created_at` timestamp NULL DEFAULT NULL COMMENT '添加时间',
`updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '广告表' ROW_FORMAT = DYNAMIC;
~~~
> 以后做别的项目 可以添加 广告位置 `position` 字段(smallint) 用数字代表不同的位置
### 代码生成
找到后台:`开发工具` -> `代码生成器` 选择 `slides` 表生成代码
系统会自动生成该表对应的增删改查
添加路由: `app\Admin\routes.php`
~~~php
...
Route::group([
...
], function (Router $router) {
...
$router->resource('/slides', 'SlideController');
});
~~~
### 创建菜单
添加一级菜单 `推广` 路径为空 添加二级菜单 `幻灯片` 路径 `/slides`
### 修改表单代码
`app/Admin/Controllers/SlideController.php`
~~~php
protected function form()
{
return Form::make(new Slide(), function (Form $form) {
$form->display('id');
$form->image('image', '图片')
->autoUpload()
->removable(false)
->move('/slide/' . date('Ymd'))
->uniqueName()
->saving(function ($v) {
return (string)$v;
})
->required();
$form->url('url')
->saving(function ($v) {
return (string)$v;
})
->required();
$form->number('sort')
->required();
$form->display('created_at');
$form->display('updated_at');
});
}
~~~
### 修改列表代码
`app/Admin/Controllers/SlideController.php`
~~~php
protected function grid()
{
return Grid::make(new Slide(), function (Grid $grid) {
$grid->column('id')
->sortable();
$grid->column('image')->image('',200,150);
$grid->column('url');
$grid->column('sort');
$grid->column('created_at');
$grid->column('updated_at')
->sortable();
$grid->filter(function (Grid\Filter $filter) {
$filter->equal('id');
});
$grid->disableViewButton();
});
}
~~~