Object of class Illuminate\Validation\Rules\Unique could not be converted to int

时间:2017-05-15 12:16:44

标签: php laravel laravel-5 laravel-5.4

I'm trying to use ignore method in laravel to apply validation on updating profile I have use ignore() for this but looks like I have gone somewhere wrong and ended up with this error can you help me out to find this here is my code. Thanks for your insights :)

User Controller

public function editProfile(Request $request) {
    $userId=$request->userId;
    $phoneNumber=$request->phoneNumber;
    if(!$request){
        $this->setMeta("422", "Nothing is there for update");
        return response()->json($this->setResponse());
    }
    $validator =Validator::make($request->all(), [
        'phoneNumber' => [
            'max:10',
            Rule::unique('users')->ignore($userId,'userId'),
        ],
    ]);
    if ($validator->fails()) {
        //$response['meta'] = array('code' => "422", 'message' => "Error, please try again");
        $errors = $validator->errors();
        if ($errors->first('phoneNumber')) {
            $message = $errors->first('phoneNumber');
        }
        $this->setMeta("403", $message);
        return response()->json($this->setResponse());
    }

    $homeTown = $request->homeTown;
    $heightInCm=0;
        /*$homeTownId= City::where('cityName', $homeTown)->first()->cityId;*/
    if($request->userHeight) {
        $userHeight=$request->userHeight;
        $heightSplit = explode("'", $userHeight, 2);
        $feet = $heightSplit[0];
        $inches = $heightSplit[1];
        $inch=($feet *12)+$inches;
        $heightInCm=$inch*2.54;
    }
    $verticalInCm=0;
    /*$homeTownId= City::where('cityName', $homeTown)->first()->cityId;*/
    if($request->userVertical) {
        $userVertical=$request->userVertical;
        $verticalSplit = explode("'", $userVertical, 2);
        $feet = $verticalSplit[0];
        $inches = $verticalSplit[1];
        $inch = ($feet *12)+$inches;
        $verticalInCm = $inch*2.54;
    }

    $data= array(
        'profilePic' => $request->profilePic,
        'nickName' => $request->nickName,
        'phoneNumber' => $request->phoneNumber,
        'userHeight' => $heightInCm,
        'userWeight' => $request->userWeight,
        'userVertical' => $verticalInCm,
        'userSchool' => $request->userSchool,
        'homeTown' => $homeTown,
        'cityId' => $request->cityId,
    );
    User::where('userId',$request->userId)->update($data);
}

2 个答案:

答案 0 :(得分:0)

检查您是否为唯一检查指定了正确的密钥。您的代码说userId是比较给定ID的主键,这是正确的吗?如果密钥默认为“id”,则可以忽略该参数。

Rule::unique('users')->ignore($$request->userId),

'phoneNumber' => 'max:10|unique:users,id,'.$request->userId,

答案 1 :(得分:0)

经过一段时间的花费,我终于得到了答案。

public function editProfile(Request $request) {
    $userId=$request->userid;
    $phoneNumber=$request->phoneNumber;
    if(!$request){
        $this->setMeta("422", "Nothing is there for update");
        return response()->json($this->setResponse());
    }
    $validator = Validator::make(
        array(
            'phoneNumber' => $phoneNumber,
        ),
        array(
            'phoneNumber' =>'size:10', Rule::unique('users')->ignore($request->userId, 'userId'),
        )
    );
    if ($validator->fails()) {
        //$response['meta'] = array('code' => "422", 'message' => "Error, please try again");
        $errors = $validator->errors();
        if ($errors->first('phoneNumber')) {
            $message = $errors->first('phoneNumber');
        }
        $this->setMeta("403", $message);
        return response()->json($this->setResponse());
    }

    $homeTown = $request->homeTown;
    $heightInCm=0;
        /*$homeTownId= City::where('cityName', $homeTown)->first()->cityId;*/
    if($request->userHeight) {
        $userHeight=$request->userHeight;
        $heightSplit = explode("'", $userHeight, 2);
        $feet = $heightSplit[0];
        $inches = $heightSplit[1];
        $inch=($feet *12)+$inches;
        $heightInCm=$inch*2.54;
    }
    $verticalInCm=0;
    /*$homeTownId= City::where('cityName', $homeTown)->first()->cityId;*/
    if($request->userVertical) {
        $userVertical=$request->userVertical;
        $verticalSplit = explode("'", $userVertical, 2);
        $feet = $verticalSplit[0];
        $inches = $verticalSplit[1];
        $inch = ($feet *12)+$inches;
        $verticalInCm = $inch*2.54;
    }

    $data= array(
        'profilePic' => $request->profilePic,
        'nickName' => $request->nickName,
        'phoneNumber' => $request->phoneNumber,
        'userHeight' => $heightInCm,
        'userWeight' => $request->userWeight,
        'userVertical' => $verticalInCm,
        'userSchool' => $request->userSchool,
        'homeTown' => $homeTown,
        'cityId' => $request->cityId,
    );
    User::where('userId',$request->userId)->update($data);
    $this->setMeta("200", "Profile Changes have been successfully saved");
    $this->setData("userDetails", $data);
    return response()->json($this->setResponse());

}