## 官方推薦寫法如下
~~~
// 根據主鍵刪除
Db::table('think_user')->delete([1,2,3]);
~~~
> delete 方法返回影響數據的條數,沒有刪除返回 0
## 刪除數據完整示例
~~~
//批量刪除
$(".select_del").click(function(){
var url = "{:url('selectDel')}";
delSelect(url);
})
//批量刪除
function delSelect(url){
if (!getCheckboxNum()){
swal(
'請先選擇要刪除的項目',
'',
'error'
)
}else{
swal({
title: '確定刪除?',
text: "刪除后將無法恢復!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3c8dbc',
cancelButtonColor: '#d33',
confirmButtonText: '確定',
cancelButtonText: '取消'
}).then(function(isConfirm) {
if (isConfirm) {
//執行刪除操作
var id = $("input:checkbox[name='key[]']:checked").map(function(index,elem) {
return $(elem).val();
}).get().join(',');
//ajax
$.post(url,{id:id},function(result){
if(result.code == 1){
swal(result.msg,'','success').then(function(){window.location.reload()});
}else{
swal(result.msg,'','error');
}
});
//ajax
}
})
}
}
//獲取Checkbox被選擇個數
function getCheckboxNum(){
var checkbox = document.getElementsByName("key[]");
var j = 0; // 用戶選中的選項個數
for(var i=0;i<checkbox.length;i++){
if(checkbox[i].checked){
j++;
}
}
return j;
}
~~~
~~~
//批量刪除
public function selectDel(){
if(Request::isPost()) {
$id = input('post.id');
if (empty($id)) {
$result['status'] = 0;
$result['msg'] = 'ID不存在!';
return $result;
}
db('link')->delete($id);
return $result = ['code'=>1,'msg'=>'刪除成功!'];
}
}
~~~
- 序言
- ThinkPHP官方資源
- 術語
- 根目錄
- php術語
- jwt
- 下載jwt
- 認識jwt
- 生成token
- 驗證token
- lcobucci/jwt
- 安裝
- 配置
- 生成token
- 解析令牌
- 驗證令牌
- 擴展庫jwt
- thinkPHP使用lcobucci/jwt
- phpmailer
- PHPMailer的使用
- phpMailer config
- 短信驗證嗎
- 阿里云短信驗證碼發送類
- 權限管理
- 基于thinkphp6.0
- 通用函數
- 密碼加密
- 數組
- 數據庫
- 查詢數據
- 添加數據
- 刪除數據
- 批量刪除
- 更新數據
- 請求流程
- thinkphp6安裝
- thinkphp6目錄介紹
- 單應用
- 多應用
- 配置文件
- 模型,模板與Model的區別
- .env介紹
- 入口文件
- 控制器
- model層
- 視圖層
- common公共函數
- 路由
- 命令行
- 常用thinkphp函數和方法
- 高德地圖i定位城市
- 更新日志