Laravel验证:调用成员函数failure()时为null [英] Laravel validation: Call to a member function fails() on null

查看:113
本文介绍了Laravel验证:调用成员函数failure()时为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码几天前就在工作,但是我似乎已经做了一些使它中断的事情.

This piece of code was working a few days ago but I seem to have done something to cause it to break.

我有这条路线:

Route::post('admin/routemanagement', 'AdminController@addRoute');

看起来像这样:

    public function addRoute(Request $request) {
        if(Auth::check()) {
            $rules = [
                'flightDep' => 'required',
                'flightArr' => 'required',
                'flightDepTime' => 'required',
                'flightArrTime' => 'required',
            ];

            $messages = [
                'flightDep.required' => 'A departure ICAO is required',
                'flightArr.required' => 'An arrival ICAO is required',
                'flightDepTime.required' => 'A departure time is required',
                'flightArrTime.required' => 'An arrival time is required'
            ];


            $validator = $this->validate($request, $rules, $messages);

            if($validator->fails()) {
                return redirect('admin/routemanagement')->withErrors($validator)->withInput();
            }
     }

但是,当运行此代码时,出于某些原因,$validator变量最终以null结束,我得到以下信息:

However when this code runs, the $validator variable ends up null for some reason and I get the following:

Call to a member function fails() on null

推荐答案

来自Laravel文档:

From the Laravel Documentation:

"...如果验证失败,则会自动生成正确的响应.如果验证通过,我们的控制器将继续正常执行. ( https://laravel.com/docs/5.2/validation )

"... if the validation fails, the proper response will automatically be generated. If the validation passes, our controller will continue executing normally." (https://laravel.com/docs/5.2/validation)

因此下面的代码不是必需的:

So the following code here is not necessary:

if($validator->fails()) {
            return redirect('admin/routemanagement')->withErrors($validator)->withInput();
        }

如果验证失败,Laravel将自动响应,如果验证成功,则其余代码将正常执行.

Laravel automatically responds if validation fails, and if it succeeds then the rest of your code executes normally.

这篇关于Laravel验证:调用成员函数failure()时为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆