The issue you're facing is that the sendEmailVerificationNotification method is not working when registering a user via API. This is because Laravel Breeze uses the MustVerifyEmail trait, which expects the user to be registered through the built-in registration routes.
To solve this problem, you need to manually send the email verification notification after creating the user. Here's how you can modify your code to achieve this:
use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Facades\Validator;
public function register(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8',
'c_password' => 'required|same:password',
]);
if ($validator->fails()) {
return response(['errors' => $validator->errors()], 422);
}
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
event(new Registered($user)); // Manually trigger the Registered event
$token = $user->createToken('MyApp')->accessToken;
return response("User successfully created");
}
By manually triggering the Registered event, you ensure that the email verification notification is sent even when registering a user via API.
Make sure to import the Registered event at the top of your file:
use Illuminate\Auth\Events\Registered;
This should resolve the issue and send the email verification notification when registering a user via API.