Skip to content

Commit 387f697

Browse files
Add URL::query() method documentation (#9598)
* Add url()->query() method documentation * Fix typo * Update urls.md --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 50d6c95 commit 387f697

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

urls.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ The `url` helper may be used to generate arbitrary URLs for your application. Th
2828

2929
// http://example.com/posts/1
3030

31+
To generate a URL with query string parameters, you may use the `query` method:
32+
33+
echo url()->query('/posts', ['search' => 'Laravel']);
34+
35+
// https://example.com/posts?search=Laravel
36+
37+
echo url()->query('/posts?sort=latest', ['search' => 'Laravel']);
38+
39+
// http://example.com/posts?sort=latest&search=Laravel
40+
41+
Providing query string parameters that already exist in the path will overwrite their existing value:
42+
43+
echo url()->query('/posts?sort=latest', ['sort' => 'oldest']);
44+
45+
// http://example.com/posts?sort=oldest
46+
47+
Arrays of values may also be passed as query parameters. These values will be properly keyed and encoded in the generated URL:
48+
49+
echo $url = url()->query('/posts', ['columns' => ['title', 'body']]);
50+
51+
// http://example.com/posts?columns%5B0%5D=title&columns%5B1%5D=body
52+
53+
echo urldecode($url);
54+
55+
// http://example.com/posts?columns[0]=title&columns[1]=body
56+
3157
<a name="accessing-the-current-url"></a>
3258
### Accessing the Current URL
3359

0 commit comments

Comments
 (0)