Skip to content
关注公众号,获取新课通知

删除权限


控制器 app/controller/admin/Rule.php

php
public function delete($id)
{
    $rule = $this->request->Model;
    return showSuccess($rule->delete());
}

验证器 app/validate/admin/Rule.php

php
// 验证规则
protected $rule = [
    ...
    'id'=>'require|integer|>:0|isExist:Rule',
    ...
];

protected $scene = [
    ...
    'delete'=>['id'],
];

模型 app/model/Rule.php

php
// 角色-规则多对多关系
public function roles(){
	return $this->belongsToMany('Role','role_rule');
}

// 清除与角色之间的关系
public function delRoles($roleId){
	return $this->roles()->detach($roleId);
}

// 关联子分类
public function childRules(){
	return $this->hasMany('Rule');
}
/**
* 删除规则之前操作
* 1. 删除角色和规则的关联关系
* 2. 删除对应的子分类
* @param [type] $rule
* @return void
*/
public static function onBeforeDelete($rule){
	// 删除角色和规则的关联关系
	$roleIds = $rule->roles->map(function($v){
		return $v->id;
	})->toArray();
	if (count($roleIds)) $rule->delRoles($roleIds);

	// 删除对应的子分类
	$rule->childRules->each(function($v){
		$v->delete();
	});
}

路由 router/admin.php

php
Route::post('rule/:id/delete','admin.Rule/delete');