Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Next1's avatar

Laravel Request Class Query String

In Laravel 5 routes.php file

    Route::get('/test',function(Request $request){
        dd( $request->all());
    });

now if I go to this route /test?a=b, result will be following

array:1 [▼
     "a" => "b"
    ]   

BUT IN LARAVEL 5.1 if I use this same code, I get following result

array:1 [▼
     "query_string" => ""
]

this works finally on my local machine but as I deployed my code on my server I'm getting this result.

i followd this guide to configure my server.

https://www.digitalocean.com/community/tutorials/how-to-install-laravel-with-an-nginx-web-server-on-ubuntu-14-04

so how to access this query string?

0 likes
19 replies
tangoG's avatar

I tested this on Laravel 5.1 and is working fine for me.

Use Illuminate\Http\Request $request instead of Request

Route::get('/', function (Illuminate\Http\Request $request) {
dd($request->all());
});

On going to route/?a=b My output is

array:1 [▼

"a" => "b" ]

karlos545's avatar

You need to add the full namespace for the Request object that you typed hinted in the closure @Dhruvin

Route::get('/test', function (Illuminate\Http\Request $request) {
    // some logic...
}

Next1's avatar

@tangoG I am using the same code but not getting result. any suggestion ? It does not to work on remote server.

Next1's avatar

@karlos545 if i dont use Illuminate\Http\Request then I will get this

Call to undefined method Illuminate\Support\Facades\Request::all()
karlos545's avatar

@Dhruvin also make sure you are referencing the right Request class.

use Illuminate\Http\Request; //this is the one that you want
Next1's avatar

@karlos545

first query() is not working and yeah its working fine on my local machine but remote server is giving me this response. check my updated question.

karlos545's avatar

I use digital ocean and this works for me, both locally and on the server...

get('/hello', function(Illuminate\Http\Request $request){
    dd($request->query());
});

gives me an array of anything after the ? in the URL @Dhruvin

Next1's avatar

if i use /test or /test?a=b I am getting the same result.

array:1 [▼
     "query_string" => ""
]

@karlos545

soltan's avatar

Did you find a solution, cuz Im having the same issue

jonathannoah's avatar

@soltan This might be too late, but I was having this issue as well and it was caused by misconfigured Nginx. Make sure that in the Nginx server block for your virtual host that the location block says:

location / {
        try_files $uri $uri/ /index.php$is_args$args;
}

The key part is $is_args$args; after index.php. This passes the query string to the application; otherwise it's never included in the $_SERVER variable.

1 like
rtk's avatar

@jonathannoah I encountered the same issue, and your example didn’t work for me as I was applying it to a query. However, I was able to resolve it.

 location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

Thank you for highlighting this. I had been going back and forth between A.I. and the Laravel documentation without success.

Please or to participate in this conversation.