'require|checkId', 'type' => 'require|in:1,2,3,4,5,6,7,8', 'name' => 'require|checkName', 'is_show' => 'in:0,1', 'sort' => 'number', ]; protected $message = [ 'id.require' => '分类id不能为空', 'name.require' => '分类名称不能为空', ]; public function sceneAdd() { return $this->only(['type','name','sort']); } public function sceneDel() { return $this->only(['id']); } public function sceneEdit() { return $this->only(['id','type','name']); } public function sceneStatus() { return $this->only(['id','is_show']); } public function sceneDetail() { return $this->only(['id']); } /** * @notes 验证分类是否已存在 * @param $value * @param $rule * @param $data * @return bool|string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author ljj * @date 2021/7/14 3:51 */ public function checkName($value,$rule,$data) { $where[] = ['name', '=', $value]; $where[] = ['type', '=', $data['type']]; // 编辑的情况,要排除自身ID if (isset($data['id'])) { $where[] = ['id', '<>', $data['id']]; } $result = InfoCategory::where($where)->select()->toArray(); if ($result) { return '该分类名称已存在'; } return true; } public function checkId($value,$rule,$data){ $where[] = ['id', '=', $value]; $result = InfoCategory::where($where)->find(); if (!$result) { return '该分类不存在!'; } return true; } }