php 传入的参数 1 是 Illuminate\Http\Request 的一个实例,给定的数组

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

Argument 1 passed t be an instance of Illuminate\Http\Request, array given

phplaravellaravel-5.2

提问by Jess McKenzie

Why am I getting the following error?

为什么我收到以下错误?

Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance 
of Illuminate\Http\Request, array given, called in 
app/Http/Controllers/Admin/Auth/AuthController.php on line 72 and defined

Functions:

职能:

protected function loginValidation($request)
    {
        $rules = array(
          'fname' => 'required|max:255',
          'lname'  => 'required|max:255',
          'email'      => 'required|email|max:255|unique:users',
          'password'   => 'required|min:6|confirmed',
    );
        $this->validate( $request , $rules);
    }
  protected function getLoginCredentials(Request $request)
  {
    $validator = $this->loginValidation(Request::all());

    var_dump($validator); die();

    if($validator->passes())
    {
    return[
    'email'    => Request::input('email'),
    'password' => Request::input('password'),
    'type'     => 1  
    ];

    return true;
    }else{
        return redirect()->back()->withErrors();
    }
  }  

Updated Code:

更新代码:

public function validate($request, $rules)
    {
        $rules = array(
          'fname' => 'required|max:255',
          'lname'  => 'required|max:255',
          'email'      => 'required|email|max:255|unique:users',
          'password'   => 'required|min:6|confirmed',
    );
        $this->validate( $request , $rules);
    }



protected function getLoginCredentials(Request $request)
  {
    $validator = $this->validate($request, $rules);

    if($validator->passes())
    {
    return[
    'email'    => Request::input('email'),
    'password' => Request::input('password'),
    'type'     => 1  
    ];

    return true;
    }else{
        return redirect()->back()->withErrors();
    }
  }  

Error:

错误:

Declaration of App\Http\Controllers\Admin\Auth\AuthController::validate() should be compatible with App\Http\Controllers\Controller::validate(Illuminate\Http\Request $request, array $rules, array $messages = Array, array $customAttributes = Array)

采纳答案by oseintow

Change to

改成

 $validator = $this->loginValidation($request);

With this you pass an instance of Request to the validate function

有了这个,您将请求的实例传递给验证函数

you are passing an array to the first argument of the validate function which i guess should be an instance of Request

您正在将数组传递给验证函数的第一个参数,我想它应该是 Request 的一个实例

$this->validate( $request , $rules);

Updated

更新

protected function loginValidation($request)
{
    $rules = array(
      'fname' => 'required|max:255',
      'lname'  => 'required|max:255',
      'email'      => 'required|email|max:255|unique:users',
      'password'   => 'required|min:6|confirmed',
);
    $this->validate( $request , $rules);
}

protected function getLoginCredentials(Request $request)
{
   $validator = $this->loginValidation($request);

   var_dump($validator); die();

   if($validator->passes())
   {
     return[
      'email'    => Request::input('email'),
      'password' => Request::input('password'),
      'type'     => 1  
  ];

      return true;
   }else{
      return redirect()->back()->withErrors();
   }
}