Laravel 5.6 - 注册表不工作且未显示任何错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49998255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.6 - Registration Form is not working and does not show any error
提问by Shimul
In one of my recent project, the custom registration formis not working. When I click on the register button, it reloads the registration form, does not print any error and no data is inserted into the database. Here is the look of the registration form:
在我最近的一个项目中,自定义注册表不起作用。当我点击注册按钮时,它会重新加载注册表,不打印任何错误并且没有数据插入到数据库中。这是注册表的外观:
Here is the migrationfile code:
这是迁移文件代码:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('fname');
$table->string('lname');
$table->string('email')->unique();
$table->string('contact');
$table->string('password');
$table->string('created_by');
$table->string('modified_by')->nullable();
$table->string('userrole');
$table->rememberToken();
$table->timestamps();
});
}
Here is the code of UserModel:
这是用户模型的代码:
protected $fillable = [
'fname', 'lname', 'email', 'password', 'contact', 'created_by', 'userrole',
];
protected $hidden = [
'password', 'remember_token', 'modified_by',
];
Here is the code of RegisterController:
这是RegisterController的代码:
protected function create(array $data)
{
return User::create([
'fname' => $data['fname'],
'lname' => $data['lname'],
'email' => $data['email'],
'contact' => $data['contact'],
'created_by' => $data['email'],
'userrole' => Config::get('constants.ROLE_USER'),
'password' => Hash::make($data['password']),
]);
}
Here is the code of constants.php, which is inside configfolder:
这是constants.php的代码,它在config文件夹中:
<?php
return array(
'ROLE_ADMIN' => 'ROLE_ADMIN',
'ROLE_USER' => 'ROLE_USER'
);
And finally, here is the code of register.blade.phpfile:
最后,这里是register.blade.php文件的代码:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header bg-dark text-white">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}">
@csrf
<div class="form-group row">
<label for="fname" class="col-md-4 col-form-label text-md-right">{{ __('First Name') }}</label>
<div class="col-md-6">
<input id="fname" type="text" class="form-control{{ $errors->has('fname') ? ' is-invalid' : '' }}" name="fname" value="{{ old('fname') }}" required autofocus>
@if ($errors->has('fname'))
<span class="invalid-feedback">
<strong>{{ $errors->first('fname') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="lname" class="col-md-4 col-form-label text-md-right">{{ __('Last Name') }}</label>
<div class="col-md-6">
<input id="lname" type="text" class="form-control{{ $errors->has('lname') ? ' is-invalid' : '' }}" name="lname" value="{{ old('lname') }}" required>
@if ($errors->has('lname'))
<span class="invalid-feedback">
<strong>{{ $errors->first('lname') }}</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">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="contact" class="col-md-4 col-form-label text-md-right">{{ __('Contact No') }}</label>
<div class="col-md-6">
<input id="contact" type="text" class="form-control{{ $errors->has('contact') ? ' is-invalid' : '' }}" name="contact" value="{{ old('contact') }}" required>
@if ($errors->has('contact'))
<span class="invalid-feedback">
<strong>{{ $errors->first('contact') }}</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">
<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 mb-0">
<div class="col-md-6 offset-md-5">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Here is a short clip of my problem: Video Clips
这是我的问题的简短剪辑:视频剪辑
So, can anyone help me to figure out, what is the actual problem? How can I solve that problem?
那么,谁能帮我弄清楚,实际问题是什么?我该如何解决这个问题?
- Thanks
- 谢谢
回答by H H
It may be because you have @csrf
and not {{ csrf_field }}
so the CSRF Token is not get posted.
这可能是因为你有@csrf
而不是{{ csrf_field }}
所以 CSRF 令牌没有被发布。
Also, just for testing you can try to add this to your blade:
另外,为了测试,您可以尝试将其添加到您的刀片中:
@if ($errors->any())
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
You can also add this to your Controller action to see exactly what is getting posted:
您还可以将其添加到您的 Controller 操作中以准确查看发布的内容:
dd(request()->all());
But be aware of any Request
s that may be doing any validation on the action
但请注意Request
可能对操作进行任何验证的任何s
回答by Saurabh Mistry
Here is the code of RegisterController:
这是RegisterController的代码:
protected function validator(array $data)
{
return Validator::make($data, [
'fname' => 'required|string|max:255',
'lname' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
protected function create(array $data)
{
return User::create([
'fname' => $data['fname'],
'lname' => $data['lname'],
'email' => $data['email'],
'contact' => $data['contact'],
'created_by' => $data['email'],
'userrole' => Config::get('constants.ROLE_USER'),
'password' => Hash::make($data['password']),
]);
}
public function register(Request $request)
{
$validation = $this->validator($request->all());
if ($validation->fails()) {
return redirect()->back()->with(['errors'=>$validation->errors()->toArray()]);
}
else{
$user = $this->create($request->all());
Auth::login($user);
return redirect('/dashboard')->with(['message'=>'Account Successfully Created.']);
}
}
in your blade view :
在您的刀片视图中:
@if (count($errors) > 0)
@foreach ($errors->all() as $error)
<p class="alert alert-danger alert-dismissible fade show" role="alert">{{ $error }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</p>
@endforeach
@endif
@if (session()->has('message'))
<p class="alert alert-success alert-dismissible fade show" role="alert">{{ session('message') }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</p>
@endif
回答by john doe
After you click the register button, if you don't want to reload the page and instead redirect you to somewhere else, and also print a message, try something like this:
单击注册按钮后,如果您不想重新加载页面而是将您重定向到其他地方,并打印一条消息,请尝试以下操作:
protected function create(array $data)
{
$user = User::create([
'fname' => $data['fname'],
'lname' => $data['lname'],
'email' => $data['email'],
'contact' => $data['contact'],
'created_by' => $data['email'],
'userrole' => Config::get('constants.ROLE_USER'),
'password' => Hash::make($data['password']),
]);
session()->flash('message', 'Thank you for registering!');
return redirect()->home();
}
回答by Chirag Patel
It is because you are returning User::create
in your controller. You have to redirect to some page like homepage or other page.
这是因为您正在返回User::create
您的控制器。您必须重定向到某个页面,例如主页或其他页面。
If you have a homepage you can do like this in your controller.
如果您有主页,则可以在控制器中执行此操作。
protected function create(Request $request)
{
$data = $request->all();
$user = User::create([
'fname' => $data['fname'],
'lname' => $data['lname'],
'email' => $data['email'],
'contact' => $data['contact'],
'created_by' => $data['email'],
'userrole' => Config::get('constants.ROLE_USER'),
'password' => Hash::make($data['password']),
]);
return redirect('home')->with('message', 'User registered!');;
}
and get message on homepage with this code,
并使用此代码在主页上获取消息,
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif