## **數據刪除自動處理**
數據刪除同樣也只需要一行代碼`$this->_delete('表名');`
前端只需要傳入數據表的主鍵就可以了,不需要做額外處理。
### **1. 硬刪除**
如果數據表中不存在`is_deleted`字段,則為硬刪除。
### **2. 軟刪除**
當前數據表中存在`is_deleted`字段時,`$this->_delete`則自動為軟刪除操作,
在列表操作時加上條件過濾下就可以正常操作。
#### 數據回調處理
對于數據刪除的結果,可以進行自定義處理,回調函數規則如:`[_ACTION]_delete_result`
下面提供一個完整的demo:
```php
/**
* 刪除系統權限
* @auth true
*/
public function remove()
{
$this->applyCsrfToken();
$this->_delete($this->table);
}
/**
* 刪除結果處理
* @param boolean $result
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
protected function _remove_delete_result($result)
{
if ($result) {
$where = ['auth' => $this->request->post('id')];
Db::name('SystemAuthNode')->where($where)->delete();
$this->success("權限刪除成功!", '');
} else {
$this->error("權限刪除失敗,請稍候再試!");
}
}
```
如果是在 ThinkAdmin 后臺基于 admin.js 的情況下,可臺使用 data-action 來與 $this->_delete 配合使用。
前端提交上來的主鍵值支持多個,以英文逗號分隔。