php - Laravel 中的注册表单重定向到自身而不创建用户实例
问题描述
我一直在用 Laravel 创建一个注册表单,我使用默认的 Auth 表单,我只将类别添加到表单中,但我不知道为什么它一直将我重定向到注册表单,甚至没有创建用户实例,我检查User.php
文件并将类别添加到错误数组中,也添加到用户迁移表中,但仍然无法正常工作
这是我的register.blade.php
文件:
@extends('layouts.app')
<!-- Fetching the categories from the DATABASE -->
<?php
use Illuminate\Support\Facades\DB;
$categories = DB::table('static_categories')->get();
?>
<!-- **************************************************** -->
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}" aria-label="{{ __('Register') }}">
@csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') }}" required autofocus>
@if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required>
@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>
@if ($errors->has('password'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
<div class="form-group row">
<label for="cat" class="col-md-4 col-form-label text-md-right">{{ __('Category') }}</label>
<div class="col-md-6">
{{-- <input id="category" type="text" class="form-control{{ $errors->has('category') ? ' is-invalid' : '' }}" name="category" value="{{ old('category') }}" required autofocus> --}}
<select id="cat" class="custom-select" required>
<option selected>Choose Category</option>
@for($i=0;$i<count($categories); $i++)
<option value="{{$categories[$i]->cat_id}}">{{$categories[$i]->categoryname}}</option>
@endfor
</select>
@if ($errors->has('cat'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('cat') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
这是我的用户迁移文件:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->integer('cat')->unsigned();
$table->foreign('cat')->references('cat_id')->on('static_categories');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
这是我的 registerController 文件:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'cat' => 'required|integer'
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'cat' => $data['cat'],
]);
}
}
非常感谢提前!不要介意发布这个问题的糟糕方式,这是我第一次!
解决方案
您缺少类别字段的名称。
<select name="cat"></select>
这就是为什么它没有按要求进行。在 laravel 中,您必须为输入字段命名,否则将被省略。我会建议,每当您遇到这种情况时,然后转储数据,例如
dd($request->all()
;
看看是否需要数据。
对您的问题的回答是: laravel 正在重定向,因为您在验证中有必需的类别字段,并且您没有将其发送到服务器,因为缺少字段名称。如果您在选择标签下方添加此代码,您可能会看到错误
@if ($errors->has('cat'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('cat') }}</strong>
</span>
@endif
推荐阅读
- javascript - 每个月底的Javascript月历错误
- machine-learning - 如何在我的 Django RestFramework 上使用我的 ML 模型?
- css - 删除或禁用其中一个 wordpress 小部件类
- javascript - Swiper滑块全屏不退出Safari
- postgresql - 将数据从 S3 文件复制到 aws postgresql:参数无效?
- c# - 从左到右顺序阅读PDF,而不是基于坐标
- 3dsmax - 在 3dsMax 中根据我们的模型和纹理的尺寸自动计算 UV Tile
- excel - excel vba reading dd/mm/yyyy as mm/dd/yyyy - pivot
- firebase - 如何让特定的 API 在 Firestore 上运行写/读请求?
- python - 在 InfluxDB 中插入高频数据的最佳方法