Laravel验证更新

时间:2016-05-09 13:02:24

标签: php laravel

当我想更新用户时,这是我的规则:

private function validateUser(Request $request,$id)
    {
        $user = [
            'firstname'             => 'required|string|max:60',
            'lastname'              => 'required|string|max:60',
            'phonenumber'           => 'required|string|max:20',
            'email'                 => 'required|unique:user,email,'.$id,
            'company_id'            => 'required|integer',
            'role_id'               => 'required|integer'
        ];

        $validator = Validator::make($request->all(), $user);

        if($validator->fails())
        {
            return $validator->errors();
        }
        return null;
    }

'email' =>'required|unique:user,email,'.$id,部分没有按照我想要的方式运作。因为当我更新用户时,我收到了json消息:

{"user":{"email":["The email has already been taken."]}}

我做错了吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

private function validateUser(Request $request,$id)
{
    $user = [
        'firstname'             => 'required|string|max:60',
        'lastname'              => 'required|string|max:60',
        'phonenumber'           => 'required|string|max:20',
        'email'                 => 'required|unique:user,id,$id',
        // otherwise you can do the following
        'email'                 => 'required|unique:user,id,{{Auth::user()->id}}',
        'company_id'            => 'required|integer',
        'role_id'               => 'required|integer'
    ];

    $validator = Validator::make($request->all(), $user);

    if($validator->fails())
    {
        return $validator->errors();
    }
    return null;
}