Laravel验证:检查两个表中是否存在

时间:2018-06-25 04:09:04

标签: php laravel validation backend

我有以下验证,我试图在两个表中检查电子邮件:

$validator = Validator::make($request->all(),
[
    'email' => "exists:users,email|exists:user_company,company_email"
]);

但是它仅适用于一个表,也就是用户表。

它周围还有吗?

2 个答案:

答案 0 :(得分:2)

可以在laravel中添加自定义验证规则的选项。在这里Laravel Custom Rules

了解更多

例如:

$validator = Validator::make($request->all(), [
            'email' => ["exists:users,email", function($attribute, $value, $fail) {
                    /* Custom Validation code */
                },],
        ]);

答案 1 :(得分:0)

尝试这种方式:

public function validator(array $data)
{
    $data = ['email2' => $data['email']];

    $rules = [   
        'email' => 'required|email|max:255|unique:users',
        'email2' => 'exists:user_company,company_email',
    ];

    $messages = [
        'email.unique' => 'This email is already exists in users table',
        'email2.exists' => 'This email is already exists in user_company table',
    ];

    return $validator = Validator::make($data, $rules, $messages);
}

$this->validator($request->all());