电子邮件已被占用,更新时 Laravel 8
Email Already Taken, When update Laravel 8
Heloo friend im dwi prihtapambudi,我在 laravel 8 上使用表单请求来创建验证,在验证中我在电子邮件上有验证唯一类型,我尝试更新数据但我有错误“电子邮件已被接收”可以请有人帮助我
这是我的模型
protected $fillable = [
'name',
'email',
'password',
];
这是我的控制器
public function store(UserRequest $request)
{
$data=$request->All();
$data['password'] = Hash::make($request->password);
User::updateOrCreate(['id'=>$request->id],$data);
return response()->json($data);
}
这是我的验证
public function rules()
{
return [
'name'=>'required|string|min:1|max:60',
'email'=>'required|string|email|unique:users|max:255',
'password'=>'required|string|min:6',
'password_confirm' => 'required|same:password',
];
}
感谢大家的关注
更新时忽略来自唯一验证的编辑用户记录行
'email'=>'required|string|email|max:255|unique:users,email,'.$this->id,
已更新:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserRequest extends FormRequest
{
private $defaultValidation=[];
public function authorize()
{
return true;
}
public function rules()
{
$this->defaultValidation=[
'name'=>'required|string|min:1|max:60',
'email'=>'required|string|email|max:255|unique:users,email,'.$this->id,
];
if(empty($this->id)){
$this->defaultValidation['password']='required|string|min:6';
$this->defaultValidation['password_confirm']='required|same:password';
}
switch ($this->method()) {
case 'POST':
return $this->defaultValidation;;
default:
break;
}
return [];
}
}
Heloo friend im dwi prihtapambudi,我在 laravel 8 上使用表单请求来创建验证,在验证中我在电子邮件上有验证唯一类型,我尝试更新数据但我有错误“电子邮件已被接收”可以请有人帮助我
这是我的模型
protected $fillable = [
'name',
'email',
'password',
];
这是我的控制器
public function store(UserRequest $request)
{
$data=$request->All();
$data['password'] = Hash::make($request->password);
User::updateOrCreate(['id'=>$request->id],$data);
return response()->json($data);
}
这是我的验证
public function rules()
{
return [
'name'=>'required|string|min:1|max:60',
'email'=>'required|string|email|unique:users|max:255',
'password'=>'required|string|min:6',
'password_confirm' => 'required|same:password',
];
}
感谢大家的关注
更新时忽略来自唯一验证的编辑用户记录行
'email'=>'required|string|email|max:255|unique:users,email,'.$this->id,
已更新:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserRequest extends FormRequest
{
private $defaultValidation=[];
public function authorize()
{
return true;
}
public function rules()
{
$this->defaultValidation=[
'name'=>'required|string|min:1|max:60',
'email'=>'required|string|email|max:255|unique:users,email,'.$this->id,
];
if(empty($this->id)){
$this->defaultValidation['password']='required|string|min:6';
$this->defaultValidation['password_confirm']='required|same:password';
}
switch ($this->method()) {
case 'POST':
return $this->defaultValidation;;
default:
break;
}
return [];
}
}