Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Ability to generate URL's with query params #51075

Merged
merged 4 commits into from
Apr 16, 2024

Conversation

stevebauman
Copy link
Contributor

This PR adds the ability to generate URL's with query parameters via a new query() method on the UrlGenerator.

Currently, URLs cannot be generated with query parameters using the UrlGenerator.

Example:

// http://localhost/products?sort=-name
url()->query('products', ['sort' => '-name']);

// http://localhost/products?columns[0]=name&columns[1]=price&columns[2]=quantity
url()->query('products', ['columns' => ['name', 'price', 'quantity']]);

Existing query parameters in the given path may also be overridden:

// http://localhost/products?sort=-price
url()->query('products?sort=-name', ['sort' => '-price']);

They may also be appended onto:

// http://localhost/products?sort=-name&search=samsung
url()->query('products?sort=-name', ['search' => 'samsung']);

@taylorotwell taylorotwell merged commit 685627d into laravel:11.x Apr 16, 2024
28 checks passed
@mrShibli
Copy link

where I write it? in model or blade file or controller or route??? where??? give me a example

@onlime
Copy link
Contributor

onlime commented Apr 18, 2024

@mrShibli

where I write it? in model or blade file or controller or route??? where??? give me a example

everywhere! That's what such helpers like url() are for, to be used in a controller, in a middleware, in blade, in wherever you wish.
You can also use url()->query('https://example.com/foo?some=name', ['search' => 'bar']) to merge query params into an existing URL with/without query params. Look at the RoutingUrlGeneratorTest examples in this PR!

@taylorotwell
Copy link
Member

@stevebauman could you send a laravel/docs PR for this one?

@stevebauman
Copy link
Contributor Author

@taylorotwell Yea absolutely!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants