## 代碼
```
<?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;
```
## 刪除前

## 刪除后
