【问题标题】:(1/1) FatalErrorException Call to a member function all() on null in laravel 5.4(1/1) FatalErrorException 在 laravel 5.4 中调用 null 上的成员函数 all()
【发布时间】:2017-07-23 14:58:11
【问题描述】:

4 我在提交时有一个表单我想验证它的字段,当我提交表单时发生了什么

 (1/1) FatalErrorException

Call to a member function all() on null

这是我下面的代码

$validator = app('validator')->make($this->request->all(),[
            'postTitle' => 'required',
            'postContent' =>'required']);

在 laravel 5.2 中这个验证器运行良好,但在 laravel 5.4 中它返回 null 有人可以帮我弄清楚这件事吗? 非常感谢任何帮助。 TIA

这是我的完整代码

<?php

namespace App\Repositories;
use App\Repositories\Contracts\addPostRepositoryInterface;
use Validator;
use Illuminate\Http\Request;

use DB;
use Session;
use Hash;

class addPostRepository implements addPostRepositoryInterface{

    protected $request;

    // Intialize request instance
    public function __contruct(Request $request){
        $this->request = $request;
    }


    public function addPosts(Request $request){
        //validate posts
        echo "test";
        $validator = Validator::make($request->all(), [
            'postTitle' => 'required',
            'postContent' =>'required',
        ]);


        //if validation fails return error response
        if($validator->fails())
            return redirect()->route('get.addPost')->withErrors($validator)->withInput();

        try{


        }catch(\Exception $e){
            return redirect()->route('get.addPost')->withErrors(["error"=>"Could not add details! Please try again."])->withInput();
        }


    }


    public function postCreate($screen){
        switch($screen){
            case 'add':
            return $this->addPosts($screen);
            break;
        }
    }

    //getAddPost View
    public function getAddPost(){
        return view('addPost');
    }

}

【问题讨论】:

  • 仅用于测试 dd($request) 告诉我它在验证部分之前包含在 addPosts 方法中吗?
  • 这是“添加”的结果
  • 嗨,你能帮我解决这个问题吗?

标签: laravel-5.4


【解决方案1】:

似乎是方法注入(在构造函数中)或其他问题。

您可以尝试在 local(addPosts()) 函数上创建请求对象。 请尝试以下替代解决方案。

<?php

namespace App\Repositories;
use App\Repositories\Contracts\addPostRepositoryInterface;
use Validator;
use DB;
use Session;
use Hash;

class addPostRepository implements addPostRepositoryInterface{


public function addPosts(Request $request){
    //validate posts
    $reqeust = new \Illuminate\Http\Request;

    $validator = Validator::make($request->all(), [
        'postTitle' => 'required',
        'postContent' =>'required',
    ]);


    //if validation fails return error response
    if($validator->fails())
        return redirect()->route('get.addPost')->withErrors($validator)->withInput();

    try{


    }catch(\Exception $e){
        return redirect()->route('get.addPost')->withErrors(["error"=>"Could not add details! Please try again."])->withInput();
    }


}


public function postCreate($screen){
    switch($screen){
        case 'add':
        return $this->addPosts($screen);
        break;
    }
}

//getAddPost View
public function getAddPost(){
    return view('addPost');
}
// REST OF THE CODE GOES HERE...
}

【讨论】:

    【解决方案2】:

    根据您提供的信息,我将向您展示如何在 Laravel 5.4 中正确验证请求

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'postTitle' => 'required',
            'postContent' =>'required',
        ]);
    
        if ($validator->fails()) {
            return redirect('your.view')
                        ->withErrors($validator)
                        ->withInput();
        }
    
        // Store the blog post...
    }
    

    这将在任何需要的地方成功地为您验证请求。如果验证失败,您将被转发到您的视图并显示相应的错误。

    确保use Validator; 在文件顶部。

    欲了解更多信息,您可以阅读https://laravel.com/docs/5.4/validation

    【讨论】:

    • 它说 (1/1) FatalErrorException Class 'App\Repositories\Validator' not found
    • 您是use Validator; 还是use Illuminate\Support\Facades\Validator;
    • 是的,这就是它所说的 (1/1) ErrorException 未定义变量:请求
    • 你通过请求了吗?显示更多代码
    • 如果你使用Validator::make($this-request-&gt;all() 那么错误信息是什么?还要确保编辑public function addPosts(Request $this-&gt;request){
    猜你喜欢
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    相关资源
    最近更新 更多