Home > Back-end >  Unique User registration response in Laravel 8
Unique User registration response in Laravel 8

Time:12-10

I want to get a JSON response when there is already a user existing with the same email id.

public function register(Request $request)
    {
        $user = new User();
        $user->name = $request->input('name');
        $user->email = $request->input('email');
        $user->password = Hash::make($request->input('password'));
        if($user->save()){
            if (Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password')])) {
                return response(['result' => true, 'user' => Auth::user()]);
            }
        }
        return response(['result' => false, 'user' => new User()]);
    }

At this time I am not getting false results. I am getting responses like the below

Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' for key 'users_email_unique' (SQL: insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) values (Pramod, [email protected], $2y$10$mB7NKRp/uyfkjHhmS3y05OdrvR.2/nGeeiuJj9JrILLGHoXUUt3ce, 2021-12-09 08:40:30, 2021-12-09 08:40:30))

Please suggest to me how can I get a response that "The user already registred with the same email id"

CodePudding user response:

you should use laravel authentication instead of making your own system i think

laravel already has LoginContoller / RegisterController and so on in it's authentication scaffolding kit

https://laravel.com/docs/8.x/starter-kits

CodePudding user response:

i think you should judge if the email is exist first, then to do next

  • Related