Laravel: Setting Request Query Parameter Dynamically on Runtime

Photo by darlene on Unsplash

Setting request query parameter can be very useful, especially in testing. We can set the request query parameter on runtime using the $request->merge() function. Here’s how:

/** @var $request \Illuminate\Http\Request */
$request = app()['request'];

'page' => '3'

In Testing Classes, we should access the app instance via $this->app :

// TestClass extending TestCase

The merge function will modify the current request in-place. If you want to clone the request, you can use the \Illuminate\Support\Facades\Request::createFrom() function

For example:

$request = app()['request'];

That’s it! Happy coding.