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

删除会员等级


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

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

模型 app/model/UserLevel.php

php
// 获取当前等级下的会员
    public function users(){
        return $this->hasMany('User');
    }
/**
     * 删除之后操作
     * 1. 初始化对应会员的user_level_id为0
     * @param [type] $userLevel
     * @return void
     */
    public static function onBeforeDelete($userLevel){
        // 获取当前等级下的所有会员
        $users = $userLevel->users;
        $users->each(function($user){
            $user->user_level_id = 3;
            $user->save();
        });
    }

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

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

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

路由 router/admin.php

php
Route::post('user_level/:id/delete','admin.UserLevel/delete');