使用laravel 5.6,在我的控制器中,我有以下代码,
$input = $request->all();
$validator = $this->validator($input);
if ($validator->passes()){
$booking = $this->create($input)->toArray();
$booking['link'] = str_random(30);
DB::table('activations')->insert(['id_user' => $booking['id'], 'token' => $booking['link']]);
Mail::send('mail.activation', $booking, function($message) use ($booking) {
$message->to($booking['email']);
$message->subject('acxian.com - Activation Code');
});
但是当我尝试提交按钮时,发生以下错误,
1/1) BadMethodCallException
Method [validator] does not exist.
我该如何解决?
答案 0 :(得分:1)
laravel控制器中没有validator
方法。要解决此错误,您可以在控制器中自己定义它,如下所示:
protected function validator($input){
return Validator::make($input, [
//array with validation rules
]);
但是验证表单数据的最佳方法是使用表单请求对象。请阅读以下内容以获取更多详细信息:https://laravel.com/docs/5.6/validation#form-request-validation