The easiest way to do this is to use the unique
of Validator rule before registering your user.
If you are using Laravel 5+, check the validation rules in your AuthController
, leave it look like this:
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'username' => 'required|unique:users',
'cpf' => 'required|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
So when there is a problem with these fields, Laravel will return a specific error message for each field at the time of registration.
Another way to do this is to check the return of each field:
$verUser = User::whereEmail($email)
->orWhereCpf($cpf)
->orWhereUsername($username)
->first();
if($verUser){
$camposRepetidos = [];
if ($verUser->username === $username) {
$camposRepetidos[] = 'Username';
}
if ($verUser->cpf === $cpf) {
$camposRepetidos[] = 'CPF';
}
if ($verUser->email === $email) {
$camposRepetidos[] = 'E-mail';
}
$mensagem = 'Um usuário já existe na base de dados.
Verifique os seguintes campos: ' . implode(',', $camposRepetidos);
Session::flash('alert-error', $mensagem);
return redirect()->to('auth/login');
}