laravel 如何使用 Session:get() 和 withErrors() 显示错误?

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

How to display error with Session:get() and withErrors()?

laravellaravel-5.2

提问by Dev

I have stack of calls in controller:

我在控制器中有一堆调用:

if ($validator->fails()) {
            return Redirect::back()
                ->withErrors($validator)
                ->withInput();
        }

The function withErrorstakes array of errors.

该函数withErrors采用错误数组。

How can I display there messages in template?

如何在模板中显示消息?

I tried:

我试过:

{{Session::get('MessageBag')}}

So, the latest edition is:

所以,最新版本是:

 $errors = $validator->messages(); // Here I get $error with fillied data
            return redirect('tour/create')
                ->withErrors($errors)
                ->withInput();

In template I do:

在模板中,我这样做:

{{count($errors)}}

It gives me zero

它给了我零

回答by Rakesh Kumar

In view file access errors with $errors

在视图文件访问错误中 $errors

{{$errors->first('MessageBag')}}

Try to print $errors print_r($errors). It is global variable for views.

尝试print $errors print_r($errors)。它是视图的全局变量。

回答by Parithiban

Try this you can see whether the data is available

试试这个你可以看到数据是否可用

  {{ dd(Request::session()) }}