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

删除角色


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

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

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

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

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

模型 app/model/Role.php

php
// 取消角色权限
public function delRules($ruleId){
	return $this->rules()->detach($ruleId);
}
/**
* 删除之前操作
* 1. 清除当前角色的所有权限
* 
* @param [type] $role
* @return void
*/
public static function onBeforeDelete($role){
	// 清除当前角色的所有权限
	$ruleIds = $role->rules->map(function($v){
		return $v->id;
	})->toArray();
	if (count($ruleIds)) $role->delRules($ruleIds);
}

路由 router/admin.php

php
Route::post('role/:id/delete','admin.Role/delete');