Laravel MessageBag 错误数组在视图中为空,但如果我杀死脚本则包含内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25323190/
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-14 09:57:04  来源:igfitidea点击:

Laravel MessageBag errors array is empty in view but with content if I kill script

validationlaravel

提问by briankip

I am trying to return errors back to my view, this is part of my controller TestcategoryController

我正在尝试将错误返回给我的视图,这是我的控制器 TestcategoryController 的一部分

    $rules =array(
        'name' => 'required'
    );
    $validator = Validator::make(Input::all(), $rules);
    //process
    if($validator->fails()){
        return Redirect::to('testcategory/create')->withErrors($validator);
    }

In the view testcategory/createif I try and output the errors like

在视图中,testcategory/create如果我尝试输出类似的错误

        @if($errors->any())         
            {{ $errors->first('name') }}
        @endif

I get nothing. But if I {{dd($errors)}}I get

我什么也得不到。但如果{{dd($errors)}}我得到

      object(Illuminate\Support\ViewErrorBag)#91 (1) { ["bags":protected]=> array(1) { 
      ["default"]=> object(Illuminate\Support\MessageBag)#92 (2) 
      { ["messages":protected]=>   array(1) 
      { ["name"]=> array(1) { [0]=> string(27) "The name field is required." } }  
      ["format":protected]=> string(8) ":message" } } }

The only way I am getting the errors is if I kill the script. What am I doing wrong?

我收到错误的唯一方法是杀死脚本。我究竟做错了什么?

回答by Manjunath Reddy

Probably another issue would be with $errors not being saved to Session variable $errors and nothing being shown in the view.

可能另一个问题是 $errors 没有被保存到会话变量 $errors 并且视图中没有显示任何内容。

Here is an example of the same issue: http://laravel.io/forum/03-28-2016-errors-variable-empty-after-failed-validation

这是同一问题的示例:http: //laravel.io/forum/03-28-2016-errors-variable-empty-after-failed-validation

For me the solution defined in the above link worked. Solution: Is in app\Http\Kernel.php Move \Illuminate\Session\Middleware\StartSession::class,from $middlewareGroups to $middleware

对我来说,上面链接中定义的解决方案有效。解决方法:是在 app\Http\Kernel.php\Illuminate\Session\Middleware\StartSession::class,从 $middlewareGroups移动 到 $middleware

Before

enter image description here

在此处输入图片说明

After

enter image description here

在此处输入图片说明

回答by kovpack

If you get nothing in your case, than it means you have overridden your $errors object with something else or with empty object - check your code and data you pass to the views. Your code is perfectly valid and works good - I've tested it locally. Maybe, you passed empty $errors object to your subview, but in other views the correct object is used.

如果你的情况一无所获,那意味着你已经用其他东西或空对象覆盖了你的 $errors 对象 - 检查你传递给视图的代码和数据。您的代码完全有效并且运行良好 - 我已经在本地对其进行了测试。也许,您将空的 $errors 对象传递给您的子视图,但在其他视图中使用了正确的对象。

回答by dyma 5p9

    Route::group(['middleware' => ['web']], function() {
    Route::get('/home', 'HomeController@index')->name('home');
    });

Add middleware, and resolved..

添加中间件,并解决..

回答by lowerends

You need to use it like this:

你需要像这样使用它:

{{ $errors->getBag('default')->first('name') }}