为什么我收到以下错误?
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
功能
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();
}
}
更新代码:
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();
}
}
错误:
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)
答案 0 :(得分:2)
更改为
$validator = $this->loginValidation($request);
使用此方法将Request实例传递给validate函数
您将数组传递给validate函数的第一个参数,我猜这应该是Request
的实例$this->validate( $request , $rules);
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();
}
}