好吧我使用流明5.4并且我对某些事情感到有些困惑,我总是使用这两个验证:
$rules = [
"field1" => "required|exists:users",
"field2" => "required|unique:users"
];
$messages = [
"field1.exists" => "Lorem ipsum dolor sit amet.",
"field2.unique" => "Lorem ipsum dolor sit amet."
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return response($validator->errors(), 400);
}
就像doc说的那样,“users”是数据库中表的名称。但是现在突然间我得到了
错误500类用户不存在
这让我很困惑,再次检查了文档并且它说要使用表名,所以我只是假设有一些更新,它现在使用Model名称,所以我尝试了。
我的所有模型都在Models文件夹中,并带有以下命名空间:App\Models;
然后我尝试了:
$rules = [
‘field1’ => "required|exists:User",
‘field2’ => "required|unique:User"
];
和
$rules = [
‘field1’ => "required|exists:\App\Models\User",
‘field2’ => "required|unique:\App\Models\User"
];
和
$rules = [
‘field1’ => "required|exists:App/Models/User",
‘field2’ => "required|unique:App/Models/User"
];
和
$rules = [
‘field1’ => "required|exists:App\\Models\\User",
‘field2’ => "required|unique:App\\Models\\User"
];
这些都不起作用,我已经用Java编写了一段时间,所以我可能忘记了我的东西而且我在这里做错了。
编辑:例外:
{
"message": "Class users does not exist.",
"code": -1,
"status_code: 500,
"debug": {
"line": 334,
"file": "/home/xxxxx/xxxxx/vendor/laravel-doctrine/orm/src/IlluminateRegistry.php",
"class": "ReflectionException",
"trace": [
"#0 /home/xxxxxx/xxxxxx/vendor/laravel-doctrine/orm/src/IlluminateRegistry.php(334): ReflectionClass->__construct('users')",
"#1 /home/xxxxxx/xxxxxx/vendor/laravel-doctrine/orm/src/Validation/DoctrinePresenceVerifier.php(123): LaravelDoctrine\\ORM\\IlluminateRegistry->getManagerForClass('users')",
"#2 /home/xxxxxx/xxxxxx/vendor/laravel-doctrine/orm/src/Validation/DoctrinePresenceVerifier.php(92): LaravelDoctrine\\ORM\\Validation\\DoctrinePresenceVerifier->getEntityManager('users')",
"#3 /home/xxxxxx/xxxxxx/vendor/laravel-doctrine/orm/src/Validation/DoctrinePresenceVerifier.php(45): LaravelDoctrine\\ORM\\Validation\\DoctrinePresenceVerifier->select('users')",
"#4 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Concerns/ValidatesAttributes.php(612): LaravelDoctrine\\ORM\\Validation\\DoctrinePresenceVerifier->getCount('users', 'email', 'xxxxxx@rock...', NULL, NULL, Array)",
"#5 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Validator.php(338): Illuminate\\Validation\\Validator->validateUnique('email', 'xxxxxx@rock...', Array, Object(Illuminate\\Validation\\Validator))",
"#6 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Validator.php(253): Illuminate\\Validation\\Validator->validateAttribute('email', 'Unique')",
"#7 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Validator.php(278): Illuminate\\Validation\\Validator->passes()",
"#8 /home/xxxxxx/xxxxxx/app/Http/Controllers/AuthController.php(50): Illuminate\\Validation\\Validator->fails()",
"#9 [internal function]: App\\Http\\Controllers\\AuthController->register(Object(Dingo\\Api\\Http\\Request))",
"#10 /home/xxxxxx/xxxxxx/vendor/illuminate/container/BoundMethod.php(29): call_user_func_array(Array, Array)",
"#11 /home/xxxxxx/xxxxxx/vendor/illuminate/container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()",
"#12 /home/xxxxxx/xxxxxx/vendor/illuminate/container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Laravel\\Lumen\\Application), Array, Object(Closure))",
"#13 /home/xxxxxx/xxxxxx/vendor/illuminate/container/Container.php(531): Illuminate\\Container\\BoundMethod::call(Object(Laravel\\Lumen\\Application), Array, Array, NULL)",
"#14 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(741): Illuminate\\Container\\Container->call(Array, Array)",
"#15 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(707): Laravel\\Lumen\\Application->callControllerCallable(Array, Array)",
"#16 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(681): Laravel\\Lumen\\Application->callLumenController(Object(App\\Http\\Controllers\\AuthController), 'register', Array)",
"#17 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(643): Laravel\\Lumen\\Application->callControllerAction(Array)",
"#18 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(623): Laravel\\Lumen\\Application->callActionOnArrayBasedRoute(Array)",
"#19 [internal function]: Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#20 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Dingo\\Api\\Http\\Request))",
"#21 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/PrepareController.php(45): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#22 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(148): Dingo\\Api\\Http\\Middleware\\PrepareController->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
"#23 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#24 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Dingo\\Api\\Http\\Request))",
"#25 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(102): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#26 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(778): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
"#27 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(624): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))",
"#28 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(598): Laravel\\Lumen\\Application->handleFoundRoute(Array)",
"#29 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(532): Laravel\\Lumen\\Application->handleDispatcherResponse(Array)",
"#30 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(781): Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}()",
"#31 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(534): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))",
"#32 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Routing/Adapter/Lumen.php(103): Laravel\\Lumen\\Application->dispatch(Object(Dingo\\Api\\Http\\Request))",
"#33 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Routing/Router.php(513): Dingo\\Api\\Routing\\Adapter\\Lumen->dispatch(Object(Dingo\\Api\\Http\\Request), 'v1')",
"#34 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/Request.php(126): Dingo\\Api\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))",
"#35 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(114): Dingo\\Api\\Http\\Middleware\\Request->Dingo\\Api\\Http\\Middleware\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#36 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(102): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#37 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/Request.php(127): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
"#38 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/Request.php(103): Dingo\\Api\\Http\\Middleware\\Request->sendRequestThroughRouter(Object(Dingo\\Api\\Http\\Request))",
"#39 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(148): Dingo\\Api\\Http\\Middleware\\Request->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
"#40 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
"#41 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\\Http\\Request))",
"#42 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(102): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
"#43 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(778): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
"#44 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(534): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))",
"#45 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(475): Laravel\\Lumen\\Application->dispatch(NULL)",
"#46 /home/disons5/public_html/xxxxxx/index.php(28): Laravel\\Lumen\\Application->run()",
"#47 {main}"
]
}
}
将我的验证规则更改为使用我的模型而不是表名后,我现在收到此错误:
{
"message": "Call to a member function createQueryBuilder() on null",
"status_code": 500,
"debug": {
"line": 93,
"file": "/home/xxxxxx/xxxxxx/vendor/laravel-doctrine/orm/src/Validation/DoctrinePresenceVerifier.php",
"class": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
"trace": [
"#0 /home/xxxxxx/xxxxxx/vendor/laravel-doctrine/orm/src/Validation/DoctrinePresenceVerifier.php(45): LaravelDoctrine\\ORM\\Validation\\DoctrinePresenceVerifier->select('App\\\\Models\\\\User')",
"#1 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Concerns/ValidatesAttributes.php(612): LaravelDoctrine\\ORM\\Validation\\DoctrinePresenceVerifier->getCount('App\\\\Models\\\\User', 'email', 'xxxxxx@rock...', NULL, NULL, Array)",
"#2 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Validator.php(338): Illuminate\\Validation\\Validator->validateUnique('email', 'xxxxxx@rock...', Array, Object(Illuminate\\Validation\\Validator))",
"#3 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Validator.php(253): Illuminate\\Validation\\Validator->validateAttribute('email', 'Unique')",
"#4 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Validator.php(278): Illuminate\\Validation\\Validator->passes()",
"#5 /home/xxxxxx/xxxxxx/app/Http/Controllers/AuthController.php(50): Illuminate\\Validation\\Validator->fails()",
"#6 [internal function]: App\\Http\\Controllers\\AuthController->register(Object(Dingo\\Api\\Http\\Request))",
"#7 /home/xxxxxx/xxxxxx/vendor/illuminate/container/BoundMethod.php(29): call_user_func_array(Array, Array)",
"#8 /home/xxxxxx/xxxxxx/vendor/illuminate/container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()",
"#9 /home/xxxxxx/xxxxxx/vendor/illuminate/container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Laravel\\Lumen\\Application), Array, Object(Closure))",
"#10 /home/xxxxxx/xxxxxx/vendor/illuminate/container/Container.php(531): Illuminate\\Container\\BoundMethod::call(Object(Laravel\\Lumen\\Application), Array, Array, NULL)",
"#11 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(741): Illuminate\\Container\\Container->call(Array, Array)",
"#12 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(707): Laravel\\Lumen\\Application->callControllerCallable(Array, Array)",
"#13 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(681): Laravel\\Lumen\\Application->callLumenController(Object(App\\Http\\Controllers\\AuthController), 'register', Array)",
"#14 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(643): Laravel\\Lumen\\Application->callControllerAction(Array)",
"#15 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(623): Laravel\\Lumen\\Application->callActionOnArrayBasedRoute(Array)",
"#16 [internal function]: Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#17 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Dingo\\Api\\Http\\Request))",
"#18 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/PrepareController.php(45): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#19 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(148): Dingo\\Api\\Http\\Middleware\\PrepareController->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
"#20 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#21 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Dingo\\Api\\Http\\Request))",
"#22 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(102): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#23 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(778): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
"#24 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(624): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))",
"#25 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(598): Laravel\\Lumen\\Application->handleFoundRoute(Array)",
"#26 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(532): Laravel\\Lumen\\Application->handleDispatcherResponse(Array)",
"#27 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(781): Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}()",
"#28 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(534): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))",
"#29 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Routing/Adapter/Lumen.php(103): Laravel\\Lumen\\Application->dispatch(Object(Dingo\\Api\\Http\\Request))",
"#30 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Routing/Router.php(513): Dingo\\Api\\Routing\\Adapter\\Lumen->dispatch(Object(Dingo\\Api\\Http\\Request), 'v1')",
"#31 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/Request.php(126): Dingo\\Api\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))",
"#32 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(114): Dingo\\Api\\Http\\Middleware\\Request->Dingo\\Api\\Http\\Middleware\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#33 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(102): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#34 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/Request.php(127): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
"#35 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/Request.php(103): Dingo\\Api\\Http\\Middleware\\Request->sendRequestThroughRouter(Object(Dingo\\Api\\Http\\Request))",
"#36 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(148): Dingo\\Api\\Http\\Middleware\\Request->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
"#37 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
"#38 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\\Http\\Request))",
"#39 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(102): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
"#40 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(778): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
"#41 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(534): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))",
"#42 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(475): Laravel\\Lumen\\Application->dispatch(NULL)",
"#43 /home/xxxxxx/public_html/xxxxxx/index.php(28): Laravel\\Lumen\\Application->run()",
"#44 {main}"
]
}
}
老实说,我甚至不熟悉laravel-doctrine我只是一直遵循laravel / lumen文档,这就是我一直用来构建我的应用程序。
答案 0 :(得分:7)
我假设您正在使用laravel-doctrine
包(来自该堆栈跟踪)。 documentation表示要进行unique
验证,您应该使用2个参数:
/**
* Store a new blog post.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$this->validate($request, [
'username' => 'required|unique:App\User,username',
]);
}
exists
验证:
/**
* Store a new blog post.
*
* @param Request $request
* @return Response
*/
public function update($id, Request $request)
{
$this->validate($request, [
'username' => 'required|exists:App\User,username',
]);
}
所以在你的情况下,它将是:
$rules = [
"field1" => "required|exists:App\User,field1",
"field2" => "required|unique:App\User,field2"
];
$messages = [
"field1.exists" => "Lorem ipsum dolor sit amet.",
"field2.unique" => "Lorem ipsum dolor sit amet."
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return response($validator->errors(), 400);
}
如果您收到错误:Call to a member function createQueryBuilder() on null
,则会忘记装饰Doctrine Metadata。在您的情况下,您需要更新App\Models\User
类:
use Doctrine\ORM\Mapping AS ORM;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
public $id;
/**
* @ORM\Column(type="string")
*/
public $field1;
/**
* @ORM\Column(type="string")
*/
public $field2;
}