Laravel 验证唯一规则反射类异常

Posted

技术标签:

【中文标题】Laravel 验证唯一规则反射类异常【英文标题】:Laravel validation unique rule reflection class exception 【发布时间】:2019-09-19 11:49:05 【问题描述】:

我正在尝试为电子邮件地址字段设置唯一规则,但我遇到了 ReflectionException,因为验证表名称被强制转换为一个类。查看文档,这应该只是将其转换为表格。..

Laravel 版本为:5.7。

验证规则如下:

$this->validate($request, [
    'email' => 'unique:users',
    'password' => 'same:password_confirmation'
]);

$this 是控制器,我也尝试过使用 Illuminate\Http\Request;结果一样。

我得到的堆栈跟踪如下:

ReflectionException thrown with message "Class users does not exist"

Stacktrace:
#65 ReflectionException in /var/www/vendor/laravel-doctrine/orm/src/IlluminateRegistry.php:362
#64 ReflectionClass:__construct in /var/www/vendor/laravel-doctrine/orm/src/IlluminateRegistry.php:362
#63 LaravelDoctrine\ORM\IlluminateRegistry:getManagerForClass in /var/www/vendor/laravel-doctrine/orm/src/Validation/DoctrinePresenceVerifier.php:137
#62 LaravelDoctrine\ORM\Validation\DoctrinePresenceVerifier:getEntityManager in /var/www/vendor/laravel-doctrine/orm/src/Validation/DoctrinePresenceVerifier.php:93
#61 LaravelDoctrine\ORM\Validation\DoctrinePresenceVerifier:select in /var/www/vendor/laravel-doctrine/orm/src/Validation/DoctrinePresenceVerifier.php:46
#60 LaravelDoctrine\ORM\Validation\DoctrinePresenceVerifier:getCount in /var/www/vendor/laravel/framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php:687
#59 Illuminate\Validation\Validator:validateUnique in /var/www/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:373
#58 Illuminate\Validation\Validator:validateAttribute in /var/www/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:268
#57 Illuminate\Validation\Validator:passes in /var/www/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:293
#56 Illuminate\Validation\Validator:fails in /var/www/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:305
#55 Illuminate\Validation\Validator:validate in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Validation/ValidatesRequests.php:47
#54 App\Http\Controllers\Controller:validate in /var/www/app/Http/Controllers/SignUpController.php:74
#53 App\Http\Controllers\SignUpController:memberSignUp in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
#52 call_user_func_array in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
#51 Illuminate\Routing\Controller:callAction in /var/www/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:45
#50 Illuminate\Routing\ControllerDispatcher:dispatch in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Route.php:212
#49 Illuminate\Routing\Route:runController in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Route.php:169
#48 Illuminate\Routing\Route:run in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php:679
#47 Illuminate\Routing\Router:Illuminate\Routing\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
#46 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/app/Http/Middleware/BoxMiddleware.php:28
#45 App\Http\Middleware\BoxMiddleware:handle in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
#44 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#43 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41
#42 Illuminate\Routing\Middleware\SubstituteBindings:handle in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
#41 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#40 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:75
#39 Illuminate\Foundation\Http\Middleware\VerifyCsrfToken:handle in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
#38 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#37 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php:49
#36 Illuminate\View\Middleware\ShareErrorsFromSession:handle in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
#35 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#34 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:63
#33 Illuminate\Session\Middleware\StartSession:handle in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
#32 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#31 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php:37
#30 Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse:handle in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
#29 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#28 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php:66
#27 Illuminate\Cookie\Middleware\EncryptCookies:handle in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
#26 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#25 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:104
#24 Illuminate\Pipeline\Pipeline:then in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php:681
#23 Illuminate\Routing\Router:runRouteWithinStack in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php:656
#22 Illuminate\Routing\Router:runRoute in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php:622
#21 Illuminate\Routing\Router:dispatchToRoute in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php:611
#20 Illuminate\Routing\Router:dispatch in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:176
#19 Illuminate\Foundation\Http\Kernel:Illuminate\Foundation\Http\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
#18 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/vendor/fideloper/proxy/src/TrustProxies.php:57
#17 Fideloper\Proxy\TrustProxies:handle in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
#16 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#15 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:31
#14 Illuminate\Foundation\Http\Middleware\TransformsRequest:handle in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
#13 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#12 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:31
#11 Illuminate\Foundation\Http\Middleware\TransformsRequest:handle in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
#10 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#9 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27
#8 Illuminate\Foundation\Http\Middleware\ValidatePostSize:handle in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
#7 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#6 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:62
#5 Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode:handle in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
#4 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\closure in /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#3 Illuminate\Routing\Pipeline:Illuminate\Routing\closure in /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:104
#2 Illuminate\Pipeline\Pipeline:then in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:151
#1 Illuminate\Foundation\Http\Kernel:sendRequestThroughRouter in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116
#0 Illuminate\Foundation\Http\Kernel:handle in /var/www/public/index.php:55

我可能只是忽略了一些东西,但是经过一小时的调试后,我决定发布一个问题。谢谢! :)

【问题讨论】:

你能添加你的控制器吗? 你能发composer.json的内容吗? 您确定users 表存在吗? 【参考方案1】:

解决我的问题的原因如下:

我的供应商文件夹中仍然保留了laravel-doctrine/orm 的一部分,这可能是因为我删除了依赖项,但从未运行 composer 来反映这些更改。因此运行composer install 解决了我的问题!

【讨论】:

【参考方案2】:

在使用 laravel-doctrine 的类型中,验证接受两个参数:

正如Documentation 所说:

/**
 * Store a new blog post.
 *
 * @param  Request  $request
 * @return Response
 */
public function store(Request $request)

    $this->validate($request, [
        'username' => 'required|unique:App\User,username',
    ]);

因此在你的情况下:

$this->validate($request, [
        'email' => 'required|unique:App\User,email',
    ]);

【讨论】:

奇怪的是,我实际上并没有使用 Doctrine。我在任何地方都没有 laravel-doctrine 依赖。 @JKaan,让我们再次查看您的 Stacktrace。因此,我建议使用此解决方案。 @JKaan,检查'doctrine_presence_verifier' => false,在doctrine.php,它必须设置为false。 @Moshen,我什至似乎没有教义.php。这应该在我认为的配置目录中? @JKaan,我再次检查了您的问题,如果您使用 Eloquent 尝试删除“laravel-doctrine”包,请使用我的解决方案。

以上是关于Laravel 验证唯一规则反射类异常的主要内容,如果未能解决你的问题,请参考以下文章

使用 laravel 验证检查唯一的日期/时间

Laravel 在身份验证异常时自动重定向

Laravel 唯一的验证规则,除了不工作

Laravel - 唯一规则验证 - getMessages 不存在

laravel 基于另一列值的唯一验证规则

Laravel 验证:带回重复记录的唯一规则