Duplicated username and error 500
In my journey to discovering Wave (and laravel outside of the cms that i know) i tried to create 2 accounts (from the backend) with the same username, obviously it doesn't works but i get an ERROR 500 page instead of some user-friendly error.
(on the frontend i get the 'username is already taken' and all works as expected)
[2022-11-28 09:27:37] local.ERROR: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Faselunare' for key 'users_username_unique' (SQL: insert into `users` (`name`, `email`, `username`, `password`, `avatar`, `settings`, `role_id`, `updated_at`, `created_at`) values (Pippo, [email protected], Faselunare, $2y$10$Tw3qyr15yz4Yd5.Ky5QUA.6vjt8mcz4c4eAFtFg/cqqp3B5Tmv4Ta, users/default.png, {"locale":null}, 1, 2022-11-28 09:27:37, 2022-11-28 09:27:37)) {"userId":1,"exception":"[object] (Illuminate\\Database\\QueryException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Faselunare' for key 'users_username_unique' (SQL: insert into `users` (`name`, `email`, `username`, `password`, `avatar`, `settings`, `role_id`, `updated_at`, `created_at`) values (Pippo, [email protected], Faselunare, $2y$10$Tw3qyr15yz4Yd5.Ky5QUA.6vjt8mcz4c4eAFtFg/cqqp3B5Tmv4Ta, users/default.png, {\"locale\":null}, 1, 2022-11-28 09:27:37, 2022-11-28 09:27:37)) at /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:760)
[stacktrace]
How can I handle this exception, causing the user to stay on the form but reporting that the username is not available?
Hi there,
The best way of doing this is to add a validation in your controller. For example:
$this->validate($request, [
'username' => 'required|unique:user|username',
]);
The unique:user|username
will check if the username exists before it allows you to do the registration.
Hope that this helps!
Best,
Bobby