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

dmag's avatar
Level 6

How to retrieve raw url query string

Is there a method in Laravel $request that retrieves raw url query string to return data like this:

par1=val1&par2=val2&par3=val3&par4=val4&par5=val5

0 likes
5 replies
bobbybouwmann's avatar
Level 88

There is no function to that. However you can write this easily by yourself.

$url = "www.example.com/posts?par1=val1&par2=val2&par3=val3&par4=val4&par5=val5"

$parsedUrl = parse_url($url);

$parsedUrl['post']; // www.example.com
$parsedUrl['path']; // /posts
$parsedUrl['query']; // par1=val1&par2=val2&par3=val3&par4=val4&par5=val5
5 likes
shez1983's avatar

what does $_GET return with no params? eg if you did var_dump($_GET)

alternatively there are some built in functions like $_SERVER['uri'] or so (check docs) which will probably give you parts of the url.. or if not you can at least get all of the URL and then explode on ? and then explode again on &

Ori's avatar

@dmitaga I wouldn't recommend to access super global variables directly, because of XSS. If you want function of Request class that returns string as you described try this.

public function someFunction(\Illuminate\Http\Request $request)
{
    $queryString = $request->getQueryString()
}
1 like
pilat's avatar
http_build_query($request->query()); // for GET params only
// or
http_build_query($request->all()); // for all data

Not only this would give you "query string, formatted for direct using in links", it would also prepare it in accordance to all related RFCs: eliminate duplicates, encode non-latin characters and so on.

2 likes

Please or to participate in this conversation.