## 代码
```
<?php
namespace app\agent\controller;
use app\agent\model\AgentCategoryModel;
use think\Controller;
use think\Db;
class AdminCategoryController extends Controller
{
/**
* 删除文章分类
*/
public function delete()
{
$agentCategoryModel = new AgentCategoryModel();
$id = $this->request->param('id');
//获取删除的内容
$findCategory = $agentCategoryModel->where('id', $id)->find();
if (empty($findCategory)) {
$this->error('分类不存在!');
}
$categoryChildrenCount = $agentCategoryModel->where('parent_id', $id)->count();
if ($categoryChildrenCount > 0) {
$this->error('此分类有子类无法删除!');
}
//该分类下的内容 如果需要按需配置
// $categoryPostCount = Db::name('portal_category_post')->where('category_id', $id)->count();
//
// if ($categoryPostCount > 0) {
// $this->error('此分类有文章无法删除!');
// }
$data = [
'object_id' => $findCategory['id'],
'create_time' => time(),
'table_name' => 'portal_category',
'name' => $findCategory['name']
];
$result = $agentCategoryModel
->where('id', $id)
->update(['delete_time' => time()]);
if ($result) {
Db::name('recycleBin')->insert($data);
$this->success('删除成功!');
} else {
$this->error('删除失败');
}
}
}
```
## 删除所需要的表
删除后会把所删除的内容记录到这张表当中!
```
DROP TABLE IF EXISTS `cmf_recycle_bin`;
CREATE TABLE `cmf_recycle_bin` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`object_id` int(11) DEFAULT 0 COMMENT '删除内容 id',
`create_time` int(10) UNSIGNED DEFAULT 0 COMMENT '创建时间',
`table_name` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '删除内容所在表名',
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '删除内容名称',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 25 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = ' 回收站' ROW_FORMAT = Compact;
```
## 删除前
![mark](http://olbhidnfa.bkt.clouddn.com/blog/20171015-011608656.png)
## 删除后
![mark](http://olbhidnfa.bkt.clouddn.com/blog/20171015-011926021.png)