Search code examples
phplaravellaravel-5laravel-validationlaravel-request

Transform Request to custom validation request


Is it possible to transform Illuminate\Http\Request to custom validation request you made with php artisan make:request MyRequest?

I would like validation to take place in a method down the road so that I have:

protected function register(Request $request)
{
   ...
   $this->userRepository->signup($request)
   ...
}

User repository:

public function signup(MyRequest $request)
{
    ...
}

Is this possible? I am getting an error now because one class is expected. Only thing that comes to mind is to make an interface, but I'm not sure if that could function.

Error I get

Type error: Argument 1 passed to UserRepository::signup() must be an instance of App\Http\Requests\MyRequest, instance of Illuminate\Http\Request given


Solution

  • I didn't find it was possible to do what I wanted even with my custom class extending the Request because naturally one method expects an instance of one class while getting another one.

    Maybe it would be possible to extract an interface out and wrap and bind it but that would be in my opinion a quickfix.

    My opinion is that concept I had was wrong from the start, and it was more of an architecture problem so I transformed my app to a different approach and manage to avoid such issues in the first place.