// 用户注册 public function register(Request $request) { $validatedData = $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:6|confirmed', ]); $user = User::create([ 'name' => $validatedData['name'], 'email' => $validatedData['email'], 'password' => Hash::make($validatedData['password']), ]); Auth::login($user); return redirect(RouteServiceProvider::HOME); } // 用户登录 public function login(Request $request) { $credentials = $request->validate([ 'email' => 'required|string|email', 'password' => 'required|string', ]); if (Auth::attempt($credentials)) { $request->session()->regenerate(); return redirect()->intended(RouteServiceProvider::HOME); } return back()->withErrors([ 'email' => 'The provided credentials do not match our 发现Laravel在不同领域的灵活运用 records.', ]); }
登录后复制