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?
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"
]
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...
}
@tangoG I am using the same code but not getting result. any suggestion ? It does not to work on remote server.
@karlos545 if i dont use Illuminate\Http\Request then I will get this
Call to undefined method Illuminate\Support\Facades\Request::all()
@Dhruvin also make sure you are referencing the right Request class.
use Illuminate\Http\Request; //this is the one that you want
@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.
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
@karlos545 I know. It supposed to work. but Its not wokring. :(
@karlos545 Im not getting anything in my log file.
just copy and paste the source code when you get the Exception @Dhruvin
if i use /test or /test?a=b I am getting the same result.
array:1 [▼
"query_string" => ""
]
@karlos545
@Dhruvin Can you post your entire routes file?
Did you find a solution, cuz Im having the same issue
@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.
@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 sign in or create an account to participate in this conversation.