The issue is not with the code you provided. The error message "failed to load response data : no data found for resource with given identifier" is related to the way Laracasts forum is displaying the response data. It seems like the response data is empty, hence the error message.
To verify that your code is working correctly, you can test it using a tool like Postman or by calling the API endpoint from your frontend application.
If you want to debug the code further, you can try removing the dd($request->all()) line and see if the error message goes away. If it does, then the issue might be related to the way the dd() function is interacting with the Laracasts forum.
Code:
{
// dd($request->all()); // remove this line
// Validate the input data using Laravel's built-in validator
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email|unique:users',
'role_id' => 'required|exists:roles,id',
]);
// If validation fails, return an error response
if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 422);
}
// Find the role based on the given role_id
$role = Role::find($request->role_id);
// validate that the role exists
if (!$role) {
return response()->json(['error' => 'Invalid role_id'], 422);
}
// validate and create new user
$user = new User([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
// Set the user's role_id and save the user
$user->role_id = $request->role_id;
$user->save();
// Create a token for the user and save it
$token = $user->createToken('authToken')->accessToken;
$user->remember_token = $token;
$user->save();
// Send an activation email to the user
// Mail::to($user->email)->send(new UserActivationEmail($token));
$mail = $user->email;
$url = env('FRONTEND_URL') . '/activate-account/' . $token;
Mail::send("Mail.UserActivation", ['token' => $token, 'url' => $url , 'user' => $user->name], function ($message) use ($mail) {
$message->to($mail);
$message->from(env('MAIL_FROM_Email'), env('MAIL_FROM_NAME'));
$message->subject('GrowthTracker Nextbridge User Activation');
});
// Return a success response with a message
return response()->json(['message' => 'Invitation Email has been sent successfully '], 200);
}