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

删除商品分类


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

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

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

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

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

模型 app/model/Category.php

php
// 关联子分类
public function categories(){
    return $this->hasMany('Category');
}
/**
     * 删除规则之前操作
     * 1. 删除关联关系
     * 2. 删除对应的子分类
     * @param [type] $category
     * @return void
     */
    public static function onAfterDelete($category){
        // 删除对应的子分类
        $category->categories->each(function($v){
            $v->delete();
        });
    }

路由 router/admin.php

php
Route::delete('category/:id','admin.Category/delete');