猿问

Laravel 注册 - 表单数据未转换为数据库

在一个新项目上工作,我使用了 laravel auth 脚手架,但我编辑了登录、注册等视图,布局以寻找美观。这样做时,提交的表单似乎可以正常工作,但是刷新数据库时不会发送任何记录。


不知道我在这里错过了什么。我将在下面包含代码。希望你能给我指明正确的方向。


HTML


@extends('layouts.app')


@section('content')


<div class="flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">

    <div class="max-w-md w-full">

        <div>

            <h2 class="mt-6 text-center text-3xl leading-9 font-extrabold text-gray-900">

              Register

            </h2>

          </div>

          <form class="mt-8" action="/register" method="POST">

            @csrf

            <input type="hidden" name="remember" value="true" />

            <div class="rounded-md shadow-sm">

              <div>

                <input aria-label="Name" name="name" type='text' required class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-t-md focus:outline-none focus:shadow-outline-blue focus:border-blue-300 focus:z-10 sm:text-sm sm:leading-5" placeholder="Full Name" />

                  </div>

                  <div class="-mt-px">

                <input aria-label="Email address" name="email" type="email" required class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-t-md focus:outline-none focus:shadow-outline-blue focus:border-blue-300 focus:z-10 sm:text-sm sm:leading-5" placeholder="Email address" />

              </div>


qq_花开花谢_0
浏览 93回答 2
2回答

开满天机

当表单从浏览器发送到您的服务器时,没有为它到达而定义的POST路由,但表单使用了该路由。尝试以下更改。/registerroutes/web.phpPOSTHTML:<form class="mt-8" action="/register/create" method="POST">网站.phpRoute::post('/register/create', 'Auth\RegisterController@create')->name('register');其次,Register控制器的create()方法不是Request从路由接收对象,而是一个未指定的名为 的数组$data。将此添加到use控制器顶部的语句中use Illuminate\Http\Request;然后像下面这样改变create(array $data)方法,所以它注入Request对象$request而不是array $data. 最后,您应该使用框架并调用实例all()上的方法来在数据库中创建记录。$request&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Create a new user instance after a valid registration.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; Request $request&nbsp; &nbsp; &nbsp;* @return \App\User&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected function create(Request $request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return User::create($request->all());&nbsp; &nbsp; }

绝地无双

也许您没有将模型存储在数据库中。尝试dump and die -> dd()一步一步地在您的create()方法中获取数据,以了解您是否正在从视图中接收数据。就像是:protected function create(array $data){&nbsp; &nbsp; // first if the $data is not empty&nbsp; &nbsp; dd($data);&nbsp; &nbsp; // then if the user is created&nbsp; &nbsp; $user = User::create([&nbsp; &nbsp; &nbsp; &nbsp; 'name' => $data['name'],&nbsp; &nbsp; &nbsp; &nbsp; 'email' => $data['email'],&nbsp; &nbsp; &nbsp; &nbsp; 'password' => Hash::make($data['password']),&nbsp; &nbsp; ]);&nbsp; &nbsp; dd($user);&nbsp; &nbsp; return $user;}如果数组不为空但 $user 为空,则问题可能来自数据库,这意味着您没有成功连接到数据库。.env使用数据库连接值修改您的文件。
随时随地看视频慕课网APP
我要回答