model.php //模型
<?php
namespace app\admin\model;
use think\Model;
class 模型名(与表名对应) extends Model;
public function getchilrenid($Id){
$Res=$this->select();
return $this->_getchilrenid($Res,$Id);
}
//根据id递归将子菜单的id放入数组,并返回数组
public function_getchilrenid($Res,$Id){
static $arr=array();
foreach ($Res as $k => $v) {
//pid在数据表中为上级id
if($v['pid'] == $Id){
$arr[]=$v['id'];
$this->_getchilrenid($Res ,$v['id']);
}
}
return $arr;
}
controller.php //控制器中删除操作
public function Del()
{
$id = input('post.id');
// dump($id);die;
$model = new Model();
$getchilrenid= $model ->getchilrenid($id);
$getchilrenid[] =(int)$id;
// dump($getchilrenid);die;
$delres = $authRule->destroy($getchilrenid);
if($delres)
{
exit(json_encode(array('code'=>0,'msg'=>'删除成功')));
}else{
exit(json_encode(array('code'=>1,'msg'=>'删除失败')));
}
}