我测试了我的应用程序以禁止重复电子邮件。它可以工作,但是无论何时发现重复,我都无法捕获该错误,或者它不会在表单页面中显示请求错误。
SQLSTATE [23000]:违反完整性约束:1062重复的条目 密钥“ admins_email_unique”的“ admin@DOMAIN.com”
我已经尝试重写RegisterController,但是仍然找不到错误首先被提取的位置。
protected function create(array $data)
{
try{
return Admin::create([
'role_id' => $data['role_id'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
catch(Illuminate\Database\QueryException $e)
{
echo($e);
}
}
我当前的RegisterController
<?php
namespace App\Http\Controllers\AdminAuth;
use App\Admin;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Traits\RoleTrait;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
use RoleTrait;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/admin/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('admin.guest');
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return Admin::create([
'role_id' => $data['role_id'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
/**
* 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, [
'role_id' => 'required|string|max:4',
'username' => 'required|string|unique:users|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
/**
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function showRegistrationForm()
{
$mgaAdminRoleNames = $this->getAdminRoleNames();
return view('admin.auth.register')->with('adminRoleNames',$mgaAdminRoleNames);
}
/**
* Get the guard to be used during registration.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
return Auth::guard('admin');
}
我已经放入admin \ auth \ register.blade.php
@if ($errors->has('email'))
<span class="invalidMessage">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
这两个解决方案之一:
1)在表单页面中显示错误,如下所示:
2)或捕获QueryException并直接回显错误
答案 0 :(得分:0)
将验证用于重复输入,希望此链接对您laravel recommended link有帮助
答案 1 :(得分:0)
我在检查验证器时找到了答案。因此,如果有人遇到此问题。解决方法如下:
protected function validator(array $data)
{
return Validator::make($data, [
'role_id' => 'required|string|max:4',
'username' => 'required|string|unique:admins|max:255',
'email' => 'required|string|email|max:255|unique:admins',
'password' => 'required|string|min:6|confirmed',
]);
}
请注意“唯一” 属性。我将其从“用户” 更改为“管理员” 。
我认为“管理员”一词来自config \ auth.php目录,您可以在其中设置防护和提供程序的内容。