@AddWebContribution It works thanks. One more question, can I also send a welcome email from that booted() function? Now, I am sending welcome emails from RegisterController after creating user:
class RegisterController extends Controller
{
public function __invoke(RegisterRequest $request)
{
// Validation & Create User
$user = User::create([
...$request->validated(),
'password' => Hash::make($request->password),
]);
// Send Welcome Email
event(new UserRegisteredEvent($user->name, $user->email));
//...
}
}
@Dmytro_Shved No, because when you call $user->delete(), Laravel will fire the deleted event after the model has been deleted from the database. At that point, the related profile may not be accessible because the user record no longer exists
Try this instead in booted() because that way you can access all relationships before the model is deleted