Search code examples
phplaravel-5.3

Conditional statement in FormRequest


In my controller I have conditional statement before I validate

if ($request->department == 5) {

   $this->validate($request, [
     //validation here
   ];

} else {

   $this->validate($request, [
     //validation here
   ];

}

Now I create FormRequest php artisan make:request DepartmentRequest

In method rules() , I don't know how to access the department parameter, I tried:

if($request->department == 1) But threw the Exception: Error - undefined $request variable.


Solution

  • You can use $this->input('department') inside your form request methods since FormRequest extends the Request class.