Laravel 中的注册表单重定向到自身而不创建用户实例
register form in Laravel redirects to itself without creating an user instance
我一直在使用 Laravel 创建注册表单,我使用默认的 Auth 表单,我只将类别添加到表单中,但我不知道为什么它一直将我重定向到注册表单甚至没有创建用户实例,我检查了 User.php
文件并将类别添加到容易出错的数组中,也添加到用户的迁移 table 中,但仍然无法正常工作
这是我的 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 正在重定向,因为您在验证中有必填类别字段,但您没有将其发送到服务器,因为缺少字段名称。如果您在 select 标签
下方添加此代码,您可能会看到错误
@if ($errors->has('cat'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('cat') }}</strong>
</span>
@endif
我一直在使用 Laravel 创建注册表单,我使用默认的 Auth 表单,我只将类别添加到表单中,但我不知道为什么它一直将我重定向到注册表单甚至没有创建用户实例,我检查了 User.php
文件并将类别添加到容易出错的数组中,也添加到用户的迁移 table 中,但仍然无法正常工作
这是我的 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 正在重定向,因为您在验证中有必填类别字段,但您没有将其发送到服务器,因为缺少字段名称。如果您在 select 标签
下方添加此代码,您可能会看到错误@if ($errors->has('cat'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('cat') }}</strong>
</span>
@endif