我正在使用唯一验证,当我对其进行测试时一切正常,但是在laravel.log中,该函数存在两个违反完整性约束的错误(键“ users_national_code_unique”的重复条目“ 5820103149”)。
验证在测试中效果很好。这是存储方法
public function store(Request $request)
{
$rules = User::rules();
unset($rules['completed_documents']);
unset($rules['type_id']);
$rules['account_number'] = 'required|string|unique:users,account_number';
$rules['city_id'] = 'required|integer|exists:cities,id';
$rules['address'] = 'nullable|string|min:20|max:200';
$this->validate($request, $rules);
$request->merge([
'type_id' => User::TYPES['phoenix'],
'status' => User::STATUS['pending'],
'password' => str_random(12),
]);
$user = User::create($request->only([
'gender',
'name',
'family',
'national_id',
'mobile',
'address',
'account_number',
'city_id',
'password',
'status',
'type_id',
]));
if (in_array($user->typeLabel, ['hp', 'qudy'])) {
$user->assignRole($user->typeLabel);
}
return response()->json($user, 201);
}
这是验证规则(简称):
public static function rules($id = null)
{
$rules = [
'national_id' => [
'required',
'digits:10',
'unique:users,national_id',
new Nationalcode
],
'completed_documents' => 'required|integer|in:0,1',
'birth_city_id' => 'nullable|integer|exists:cities,id',
'account_number' => 'nullable|string|unique:users,account_number',
];
if ($id) {
$rules['national_id'] = [
'required',
'digits:10',
'unique:users,national_id,' . $id,
new Nationalcode
];
}
return $rules;
}
错误:
[2019-04-12 19:26:54] production.ERROR: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '5820103149' for key 'users_national_id_unique' (SQL: insert into `users` (`meta`, `history`, `gender`, `name`, `family`, `national_id`, `mobile`, `address`, `account_number`, `city_id`, `password`, `status`, `type_id`, `updated_at`, `created_at`) values ({}, {}, 1, ح*****ن, ع***ب, 5***149, 0*****9, شهرستان جویب****ستان, 6037****8901522, 180, $2y$10$fSLw0o8J1JkVWKBWLQ7m3eQQQQiByk.7beRf4vQrcB3Wbb38IpJEu, 2, 3, 2019-04-12 19:26:54, 2019-04-12 19:26:54)) {"exception":"[object] (Illuminate\\Database\\QueryException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '5820103149' for key 'users_national_id_unique' (SQL: insert into `users` (`meta`, `history`, `gender`, `name`, `family`, `national_id`, `mobile`, `address`, `account_number`, `city_id`, `password`, `status`, `type_id`, `updated_at`, `created_at`) values ({}, {}, 1, ح****ن, ع***ب, 5820103149, 09337956089, شهرستان جو******ان, 6037997298901522, 180, $2y****w0o8J1JkVWKB******vQrcB3Wbb38IpJEu, 2, 3, 2019-04-12 19:26:54, 2019-04-12 19:26:54)) at /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, Doctrine\\DBAL\\Driver\\PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '5820103149' for key 'users_national_id_unique' at /home/quantum/web/quantumco.ir/public_html/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:119, PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '5820103149' for key 'users_national_id_unique' at /home/quantum/web/quantumco.ir/public_html/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:117)
[stacktrace]
#0 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(624): Illuminate\\Database\\Connection->runQueryCallback('insert into `us...', Array, Object(Closure))
#1 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(459): Illuminate\\Database\\Connection->run('insert into `us...', Array, Object(Closure))
#2 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(411): Illuminate\\Database\\Connection->statement('insert into `us...', Array)
#3 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php(32): Illuminate\\Database\\Connection->insert('insert into `us...', Array)
#4 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2494): Illuminate\\Database\\Query\\Processors\\Processor->processInsertGetId(Object(Illuminate\\Database\\Query\\Builder), 'insert into `us...', Array, 'id')
#5 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1283): Illuminate\\Database\\Query\\Builder->insertGetId(Array, 'id')
#6 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(787): Illuminate\\Database\\Eloquent\\Builder->__call('insertGetId', Array)
#7 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(752): Illuminate\\Database\\Eloquent\\Model->insertAndSetId(Object(Illuminate\\Database\\Eloquent\\Builder), Array)
#8 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(615): Illuminate\\Database\\Eloquent\\Model->performInsert(Object(Illuminate\\Database\\Eloquent\\Builder))
#9 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(755): Illuminate\\Database\\Eloquent\\Model->save()
#10 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Support/helpers.php(1041): Illuminate\\Database\\Eloquent\\Builder->Illuminate\\Database\\Eloquent\\{closure}(Object(App\\Models\\User))
#11 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(756): tap(Object(App\\Models\\User), Object(Closure))
#12 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1570): Illuminate\\Database\\Eloquent\\Builder->create(Array)
#13 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1582): Illuminate\\Database\\Eloquent\\Model->__call('create', Array)
#14 /home/quantum/web/quantumco.ir/public_html/app/app/Http/Controllers/UserController.php(43): Illuminate\\Database\\Eloquent\\Model::__callStatic('create', Array)
#15 [internal function]: App\\Http\\Controllers\\UserController->store(Object(Illuminate\\Http\\Request))
#16 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
#17 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('store', Array)
#18 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Route.php(212): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\UserController), 'store')
#19 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Route.php(169): Illuminate\\Routing\\Route->runController()
#20 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(665): Illuminate\\Routing\\Route->run()
#21 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#22 /home/quantum/web/quantumco.ir/public_html/app/app/Http/Middleware/CheckUserStatus.php(28): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#23 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): App\\Http\\Middleware\\CheckUserStatus->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#24 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#25 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#26 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#27 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#28 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(67): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#29 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#30 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#31 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#32 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#33 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#34 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(63): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#35 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Session\\Middleware\\StartSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#36 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#37 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#38 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#39 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#40 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#41 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#42 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#43 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#44 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(667): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#45 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(642): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#46 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(608): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#47 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(597): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#48 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#49 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#50 /home/quantum/web/quantumco.ir/public_html/app/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php(58): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#51 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Barryvdh\\Debugbar\\Middleware\\InjectDebugbar->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#52 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#53 /home/quantum/web/quantumco.ir/public_html/app/app/Http/Middleware/PreferredDomain.php(32): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#54 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): App\\Http\\Middleware\\PreferredDomain->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#55 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#56 /home/quantum/web/quantumco.ir/public_html/app/app/Http/Middleware/ModifyRequest.php(51): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#57 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): App\\Http\\Middleware\\ModifyRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#58 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#59 /home/quantum/web/quantumco.ir/public_html/app/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#60 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#61 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#62 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(31): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#63 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#64 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#65 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(31): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#66 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#67 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#68 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#69 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#70 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#71 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#72 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#73 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#74 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#75 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#76 /home/quantum/web/quantumco.ir/public_html/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#77 /home/quantum/web/quantumco.ir/public_html/app/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#78 {main}
"}
答案 0 :(得分:0)
您必须忽略自我以防止出现此错误:
'FIELD' => 'unique:table,email_column_to_check,id_to_ignore'