laravel 注册后MySQL数据库未更新

8fsztsew  于 2022-11-18  发布在  Mysql
关注(0)|答案(2)|浏览(139)

首先,我是新的Laravel框架,所以请耐心等待我。我正在创建一个电子商务网站,并创建了一个注册页面。问题是每当我填写注册表,并按下注册按钮,页面将刷新,但我填写的数据不会在MySQL数据库中更新。
注册文件.registration.blade.php

@extends('layouts.master')
@section('content')
@section('title', 'Register')

    <section class="login-page"> 
        <div class="login-form-box">
            <div class="login-title"> Register</div>
            <div class="login-form">
                <form action="{{route('register')}}" method="post">
                @csrf
                    <div class="field">
                        <label for="name">name</label>
                        <input type="text" id="name" name="name" placeholder="john" >

                    </div>

                    <div class="field">
                        <label for="email">email</label>
                        <input type="email" id="email" name="name" placeholder="john@gmail.com" >

                    </div>

                    <div class="field">
                        <label for="password">password</label>
                        <input type="password" id="password" name="password" placeholder="******" >

                    </div>

                    <div class="field">
                        <label for="passowrd_confirmation">confirm password</label>
                        <input type="password" id="password" name="passowrd_confirmation" placeholder="******" >

                    </div>

                    <div class="field">
                        <button type= "submit" class="btn btn-primary btn-block">Register</button>
                    </div>
                    

                </form>
            </div>
        </div>

    </section>
@endsection

'同时,这里是laravel中的路由文件

网页.php

<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\PagesController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', [PagesController::class, 'home'])->name('home');
Route::get('/cart', [PagesController::class, 'cart'])->name('cart');

//Auth
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
Route::post('/register', [AuthController::class, 'postRegister'])->name('register');

下面是注册函数的文件AuthController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{
    //show login page
    public function showLogin()
    {
        return view('pages.login');
    }

    //show register page
    public function showRegister()
    {
        return view('pages.register');
    }

     //register user
    public function postRegister(Request $request)
     {
        //validation
        $request->validate([
            'name' => 'required|min:3|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:8|confirmed'
        ]);

        //registration
        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),

        ]);

        //login user
        Auth::login($user);

        return back()->with('success','succesfully Logged in');
        
     }
    

    
}

'以下是我在phpmyadmin中填写表单后的数据库:users database我试着在AuthController.php中使用showRegister函数来检查是否可以在网页中看到正在填充的数据。但是,我什么都看不到。

e4yzc0pl

e4yzc0pl1#

显示错误和旧值

首先,我建议您更新视图,以便在使用验证时显示错误,并显示旧值:

<div class="field">
  <label for="name">name</label>
  <input type="text" id="name" name="name" value="{{ old('name') }}" placeholder="john" >
  @error('name')
    <div>{{$message}}</div>
  @enderror
</div>

如果验证不成功,则不会进行任何更改。尝试显示验证是否通过:

public function postRegister(Request $request)
{
  //validation
  $request->validate([
    'name' => 'required|min:3|max:255',
    'email' => 'required|email|max:255|unique:users',
    'password' => 'required|min:8|confirmed'
  ]);

  dd("Validation passed!);
}

这将帮助您查找错误。

看看the official Laravel validation documentation

wmvff8tz

wmvff8tz2#

尝试检查registration.blade.php文件中的验证错误消息

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

相关问题