未定义变量:Laravel 中的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34454081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:12:58  来源:igfitidea点击:

Undefined variable: errors in Laravel

laravellaravel-5laravel-validationlaravel-middlewarelaravel-5.2

提问by Anhinga

When I want to register a user in my laravel project, the page always says

当我想在我的 Laravel 项目中注册用户时,页面总是说

Undefined variable: errors (View: /var/www/resources/views/auth/register.blade.php)"

未定义变量:错误(查看:/var/www/resources/views/auth/register.blade.php)”

According to the Laravel documentation, $errorsshould always automatically be set:

根据 Laravel 文档,$errors应始终自动设置:

So, it is important to note that an $errors variable will always be available in all of your views on every request, allowing you to conveniently assume the $errors variable is always defined and can be safely used.

因此,重要的是要注意 $errors 变量在每个请求的所有视图中始终可用,允许您方便地假设 $errors 变量始终已定义并且可以安全使用。

I have this on on every view when I use:

当我使用时,我在每个视图上都有这个:

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

or any other way when I want to use the $errorsvariable.

或者当我想使用$errors变量时的任何其他方式。

Why is this? I never had this problem before.

为什么是这样?我以前从未遇到过这个问题。

Can someone help me please?

有人能帮助我吗?

回答by Marcin Nabia?ek

You should make sure that in app/Http/Kernel.phpin middlewareGroupsproperty for webyou have:

你应该确保在app/Http/Kernel.phpmiddlewareGroups属性web您有:

\Illuminate\View\Middleware\ShareErrorsFromSession::class,

in this array. Compare this with https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php

在这个数组中。将此与https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php进行比较

EDIT

编辑

It seems you need to add 'middleware' => 'web'for route you are using or put \Illuminate\View\Middleware\ShareErrorsFromSession::class,into $middlewareproperty array

看来你需要添加'middleware' => 'web'你所使用的路线或将\Illuminate\View\Middleware\ShareErrorsFromSession::class,进入$middleware属性数组

or

或者

Inside of the routes.php file try to create your routes within the following block

在 routes.php 文件中,尝试在以下块中创建您的路由

Route::group(['middleware' => ['web']], function () {
    //routes here
});

UPDATE FOR NEWER VERSIONS OF LARAVEL APPLICATION

更新 Laravel 应用程序的新版本

Be aware that you might run into problems also in case you use webmiddleware twice. There was a change in Laravel application 5.2.27(don't confuse it with Laravel framework you use at the moment - you might use Laravel framework for example 5.2.31 but have Laravel application in version 5.2.24) in which webmiddleware is applied automatically for all routes. So in case of problems, you should open your app/Providers/RouteServiceProvider.phpfile and verify its content.

请注意,如果您web两次使用中间件,您也可能会遇到问题。Laravel 应用程序5.2.27(不要将它与您目前使用的 Laravel 框架混淆 - 您可能使用 Laravel 框架,例如 5.2.31,但在版本 5.2.24 中使用 Laravel 应用程序),其中web应用了中间件自动为所有路线。因此,如果出现问题,您应该打开app/Providers/RouteServiceProvider.php文件并验证其内容。

You can compare it also here :

您也可以在此处进行比较:

In case you have newer version (that applies webmiddleware automatically), you shouldn't use webmiddleware in routes.phpanymore or you should modify your RouteServiceProvidermethod to not apply webgroup middleware. Otherwise if webmiddleware group is automatically applied in this provider and you use it also in routes.phpyou might get very unexpected results.

如果你有更新的版本(web自动应用中间件),你不应该再使用web中间件,routes.php或者你应该修改你的RouteServiceProvider方法来不应用web组中间件。否则,如果web在此提供程序中自动应用中间件组并且您也在其中使用它,routes.php您可能会得到非常意外的结果。

回答by Felipe Pe?a

I had this very same issue with Laravel 5.2.x.

我在 Laravel 5.2.x 上遇到了同样的问题。

Inside of the routes.phpfile try yo create your routes within the

routes.php文件内部尝试创建您的路线

Route::group(['middleware' => ['web']], function () {
    //routes here
}

statement.

陈述。

回答by Krzysztof Boduch

Also to be aware of: If you write tests and your view has$errors variable make sure you don't use WithoutMiddlewaretrait.

还要注意:如果您编写测试并且您的视图具有$errors 变量,请确保您不使用withoutMiddleware特性。

回答by Tom11

I had similar problem and solved this one by adding routes into middleware property array as well,

我遇到了类似的问题,并通过将路由添加到中间件属性数组中来解决这个问题,

BUT

it worked only after calling php artisan route:cache(clearing route cache) subsequently.

它仅在php artisan route:cache随后调用(清除路由缓存)后才起作用。

I hope some of you would find this useful.

我希望你们中的一些人会发现这很有用。

回答by tisuchi

Go to App\Http\Kernel.phpfile. Move all the things of $middlewareGroupsproperties to $middleware.

转到App\Http\Kernel.php文件。将所有$middlewareGroups属性的东西移到$middleware.

Check for more details- http://www.tisuchi.com/laravel-5-2-undefined-variable-error-validation/

检查更多细节 - http://www.tisuchi.com/laravel-5-2-undefined-variable-error-validation/

回答by Nikush

I was seeing this error too and later realised I had used the WithoutMiddlewaretrait as a means to bypass authentication for this particular test, but it ended up removing the validation error binding too. So I had to stop using the trait to keep the views working.

我也看到了这个错误,后来意识到我已经使用这个WithoutMiddleware特征来绕过这个特定测试的身份验证,但它最终也删除了验证错误绑定。所以我不得不停止使用这个特性来保持视图的工作。

回答by Vojko

count is not really realiable since it assumes the variable already exists. change the condition check to: @if($errors->has())or just @if($errors)

count 并不是真的可行,因为它假设变量已经存在。将条件检查更改为:@if($errors->has())或只是@if($errors)

Also if you are redirecting make sure to use this in your controller

此外,如果您要重定向,请确保在您的控制器中使用它

return redirect()->back()->with('errors', $validator->messages());

EDIT: seen now that you are using L5.2 This may answer your question - you need to put your Routes in Route group.

编辑:现在看到您正在使用 L5.2 这可能会回答您的问题 - 您需要将您的路线放在路线组中。

Laravel 5.2 validation errors

Laravel 5.2 验证错误

回答by Amit Rai

protected $middleware = [              \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Social\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \Social\Http\Middleware\VerifyCsrfToken::class,
];

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [

    ],

    'api' => [
        'throttle:60,1',
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.

make your kernel look like this

让你的内核看起来像这样

回答by Asadullah Al Galib

Your problem will be fixed by using this method.

使用此方法将解决您的问题。

Route::group(['middleware' => ['web']], function () {
        //routes should go here
});

If this doesn't help you, just run the following artisan command in addition to the above code:

如果这对您没有帮助,除了上面的代码外,只需运行以下 artisan 命令:

php artisan key:generate

I solved in this way while using 5.2.*

我在使用 5.2.* 时以这种方式解决了

回答by Brijesh Hadiyal

It was not actually error, it was warning you can remove using php error_reporting()function

这实际上不是错误,而是警告您可以使用 phperror_reporting()函数删除

I removed warning in my Laravel 5.6 by using

我通过使用删除了 Laravel 5.6 中的警告

error_reporting(E_ALL ^ E_NOTICE);

error_reporting(E_ALL ^ E_NOTICE);

More Detail : http://php.net/manual/en/function.error-reporting.php

更多细节:http: //php.net/manual/en/function.error-reporting.php

Hope it helps.

希望能帮助到你。