RegisteredUserController.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\User;
  5. use App\Providers\RouteServiceProvider;
  6. use Illuminate\Auth\Events\Registered;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\Hash;
  10. use Illuminate\Validation\Rules;
  11. class RegisteredUserController extends Controller
  12. {
  13. /**
  14. * Display the registration view.
  15. *
  16. * @return \Illuminate\View\View
  17. */
  18. public function create()
  19. {
  20. return view('auth.register');
  21. }
  22. /**
  23. * Handle an incoming registration request.
  24. *
  25. * @param \Illuminate\Http\Request $request
  26. * @return \Illuminate\Http\RedirectResponse
  27. *
  28. * @throws \Illuminate\Validation\ValidationException
  29. */
  30. public function store(Request $request)
  31. {
  32. $request->validate([
  33. 'name' => ['required', 'string', 'max:255'],
  34. 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
  35. 'password' => ['required', 'confirmed', Rules\Password::defaults()],
  36. ]);
  37. $user = User::create([
  38. 'name' => $request->name,
  39. 'email' => $request->email,
  40. 'password' => Hash::make($request->password),
  41. ]);
  42. event(new Registered($user));
  43. Auth::login($user);
  44. return redirect(RouteServiceProvider::HOME);
  45. }
  46. }