Search code examples
phplaravellaravel-validationlaravel-request

Laravel: Use two Requests at the same time in a controller


I have a function in my controller. The problem is I must use two Requests at the same time but only one of them can be used in a controller.

  • Illuminate\Support\Facades\Request
  • Illuminate\Http\Request

Code:

public function func(Request $req) {
    if (Request::isMethod('post')) {
        $this->validate($req, [
            'username' => 'required|string'
        ]);
    }
}

What is the solution?


Solution

  • If you wanted to use both of them, you can alias them as below:

    use Illuminate\Http\Request as RequestNew;
    use Illuminate\Support\Facades\Request as RequestOld;
    

    And then you can reference the alias in your code.

    eg: RequestNew::isMethod('post')