【laravel】laravel+mysql注册登录

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            form
            { 
                width:100%;
                height:100%;
                margin-top: 100px;
				margin-bottom: 100px;
                background:#008B8B;
            }
            div
            {
                display:inline-block;
                padding-top: 255px;
                padding-bottom: 255px;
				padding-left: 1px;
				padding-right: 1px;
            }
            h2
            {
                font-family: "微软雅黑";
            font-size: 40px;
                color:black;
            }
            #log
            {
                color:blue;
            }
        </style>
    </head>
    <body>
        <form action="{{route('user.store')}}" method="post" >
	@csrf
		    <center>
            <div>
            <h3>
            注册
			</h3>
			<p>
				用户名:<input type="text" name="username" />
			</p>
			<p>&emsp;码:<input type="password" name="password" />
			</p>
			<p>
				<input id=reg type="submit" value="立即注册" />
			</p>
			<p>
				已有账号?<a href="login.html">请登录</a>
			</p>   
            </div>
            </center>			
        </form>      
    </body>
</html>

public function store(Request $request)
    {
        //
        
        $username = $request->input('username');
        $password = $request->input('password');
       $bool = DB::insert('insert into register (username,password) value (?,?)',[$username,$password]);
       return view('store',['bool' => $bool]);
        // return view('store',['name' => $username],['password' => $password]);
    }
首先,你需要创建一个用户表来存储用户的信息,包括用户名密码和邮箱等。可以使用Laravel自带的迁移工具来创建表。在命令行中输入`php artisan make:migration create_users_table --create=users`,然后打开创建的迁移文件,添加需要的字段。 ``` Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); ``` 接下来,需要创建一个用户模型,并将其关联到用户表。在命令行中输入`php artisan make:model User`,然后打开创建的用户模型文件,在类中添加以下代码: ``` class User extends Authenticatable { use Notifiable; protected $fillable = [ 'name', 'email', 'password', ]; protected $hidden = [ 'password', 'remember_token', ]; public function getEmailForPasswordReset() { return $this->email; } } ``` 然后,你需要创建一个控制器来处理注册和登录逻辑。在命令行中输入`php artisan make:controller AuthController`,然后打开创建的控制器文件,在类中添加以下代码: ``` class AuthController extends Controller { public function showRegistrationForm() { return view('auth.register'); } public function register(Request $request) { $this->validate($request, [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:6|confirmed', ]); $user = User::create([ 'name' => $request->name, 'email' => $request->email, 'password' => Hash::make($request->password), ]); Auth::login($user); return redirect('/home'); } public function showLoginForm() { return view('auth.login'); } public function login(Request $request) { $this->validate($request, [ 'email' => 'required|string|email', 'password' => 'required|string', ]); if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) { return redirect()->intended('/home'); } return back()->withErrors(['email' => 'Email or password is incorrect.']); } public function logout() { Auth::logout(); return redirect('/login'); } } ``` 在这个控制器中,我们定义了四个方法:`showRegistrationForm()`用于显示注册表单,`register()`用于处理注册逻辑,`showLoginForm()`用于显示登录表单,`login()`用于处理登录逻辑,`logout()`用于退出登录。 最后,你需要创建两个视图文件,一个用于显示注册表单,一个用于显示登录表单。在`resources/views/auth`目录下分别创建`register.blade.php`和`login.blade.php`视图文件。在这两个文件中,你可以使用Laravel自带的表单生成器来创建表单。 注册表单: ``` <form method="POST" action="{{ route('register') }}"> @csrf <div> <label for="name">Name</label> <div> <input id="name" type="text" name="name" value="{{ old('name') }}" required autofocus> </div> @if ($errors->has('name')) <span> <strong>{{ $errors->first('name') }}</strong> </span> @endif </div> <div> <label for="email">Email</label> <div> <input id="email" type="email" name="email" value="{{ old('email') }}" required> </div> @if ($errors->has('email')) <span> <strong>{{ $errors->first('email') }}</strong> </span> @endif </div> <div> <label for="password">Password</label> <div> <input id="password" type="password" name="password" required> </div> @if ($errors->has('password')) <span> <strong>{{ $errors->first('password') }}</strong> </span> @endif </div> <div> <label for="password-confirm">Confirm Password</label> <div> <input id="password-confirm" type="password" name="password_confirmation" required> </div> </div> <div> <div> <button type="submit"> Register </button> </div> </div> </form> ``` 登录表单: ``` <form method="POST" action="{{ route('login') }}"> @csrf <div> <label for="email">Email</label> <div> <input id="email" type="email" name="email" value="{{ old('email') }}" required autofocus> </div> @if ($errors->has('email')) <span> <strong>{{ $errors->first('email') }}</strong> </span> @endif </div> <div> <label for="password">Password</label> <div> <input id="password" type="password" name="password" required> </div> @if ($errors->has('password')) <span> <strong>{{ $errors->first('password') }}</strong> </span> @endif </div> <div> <div> <button type="submit"> Login </button> </div> </div> </form> ``` 这样,你就可以在Laravel中实现用户Email注册和登录了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值