laravel 基础的接口请求验证

 * 更新用户基本信息
    public function UpdateProfiles(Request $request)
    {
        $validator = $this->setRules([
            'ss' => 'required|string',
            'sex' => 'integer|in:1,2', // 1男  2 女
            'address' => 'string',
            'name' => 'string',
            'birthday'=> [
                'string',
                'regex:/^(19|20)\d{2}-(1[0-2]|0?[1-9])-(0?[1-9]|[1-2][0-9]|3[0-1])$/'
            ],
        ])
            ->_validate($request->all());
        if (!$validator) throw new ValidationErrorException;
        $user_id = $this->getUserIdBySession($request->ss); //获取用户id

        $tmp = [];
        $params_arr = ["sex","address","name","birthday"];
        foreach($request->all() as $k=>$v){
            if(in_array($k,$params_arr)){
                  $tmp[$k] = $v;
            }
        }
        if(empty($tmp)){ //没有要更新的内容 1049
            return $this->setStatusCode(1049)->respondWithError($this->message);
        }

        $res = \DB::table('ys_member')->where('user_id',$user_id)->update($tmp);
        if($res === false){
            return $this->setStatusCode(9998)->respondWithError($this->message);

        }else{
            return $this->respond($this->format([],true));
        }


    }

猜你喜欢

转载自blog.csdn.net/qq_25861247/article/details/85618092