我有一个表单,管理员可以在其中添加用户。当我使用数据库中存在的某些电子邮件提交表单时,出现错误提示
SQLSTATE [23000]:违反完整性约束:1062项'users_email_unique'的重复条目'mail@mail.com'
我想避免该错误,而是例如获得警告“邮件已接收”或类似内容。任何帮助表示赞赏。这是我的代码。
UserController.php
public function store(StoreUserInfo $data)
{
$data->validated();
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'city' => $data['city']
]);
return redirect()
->route('admin.users')
->with('message', 'User created successfully');
}
StoreUserInfo.php
public function rules()
{
$emailid = (Auth::user()->roles()->first()->name == 'admin')
? (isset($this->user->id)?$this->user->id:Auth::user()->id) : Auth::user()->id;
return [
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,' . $emailid],
'city' => ['required', 'exists:cities,city']
];
}
register.blade.php
<form id="form"method="POST" action="{{ route('admin.user.store') }}">
@csrf
<div class="form-group row">
<label for="first_name" class="col-md-4 col-form-label text-md-right">{{ __('First Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control @error('first_name') is-invalid @enderror" name="first_name" value="{{ old('first_name') }}" required autocomplete="first_name" autofocus>
@error('first_name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="last_name" class="col-md-4 col-form-label text-md-right">{{ __('Last Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control @error('last_name') is-invalid @enderror" name="last_name" value="{{ old('last_name') }}" required autocomplete="last_name" autofocus>
@error('last_name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</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 @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</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 @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</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 autocomplete="new-password">
</div>
</div>
<div class="form-group row">
<label for="city" class="col-md-4 col-form-label text-md-right">{{ __('Select City') }}</label>
<input name="city" list="result" id="input" value="{{ old('city') }}" class="form-control @error('city') is-invalid @enderror col-sm-6 custom-select custom-select-sm"required autocomplete="city">
<datalist id="result">
</datalist>
@error('city')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</form>
用户表
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('city');
$table->rememberToken();
$table->timestamps();
});
}
答案 0 :(得分:1)
您可以这样检查。
public function store(StoreUserInfo $data)
{
$data->validated();
$email = User::where('email',$data['email'])->first();
if($email){
return redirect()
->route('admin.users')
->with('message', 'Email is already exists.');
}
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'city' => $data['city']
]);
return redirect()
->route('admin.users')
->with('message', 'User created successfully');
}
答案 1 :(得分:1)
这可以通过几种不同的方法来处理,但是为了提高可读性并使查询数量保持最小,我将create()
更改为firstOrCreate()
并使用内置于{{1} },以查看该用户是否是新用户。
wasRecentlyCreated
由于您可以将两个数组传递给 $user = User::firstOrCreate([
'email' => $data['email'],
], [
'password' => Hash::make($data['password']),
'city' => $data['city']
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
]);
,因此它将返回与电子邮件匹配的第一条记录,或使用传递给它的属性创建一个新记录。然后,您可以使用firstOrCreate
来检查该记录是否为新记录,或者是否已经存在。
wasRecentlyCreated
答案 2 :(得分:1)
您可以使用Rule创建验证器 https://laravel.com/docs/5.8/validation#rule-unique
$messages = [
'email.unique' = 'Email taken',
];
Validator::make($email, [
'email' => [
'required',
Rule::unique('users')->where(function ($query) use($email) {
return $query->where('email', $email);
}),
],
],
$messages
);
这将帮助您管理所有验证,而不仅仅是一个。如果添加其他验证,您将返回给用户所有错误,而不仅仅是电子邮件错误。