Nabil Farhan
Nabil Farhan

Reputation: 1536

How to add extra parameter with full url in laravel blade?

I have a url with some query string. I know I can get the full string with url()->full(). But I want to add extra query string with it. And there I am facing problem.

So far I have tried, href={{ url()->full()."&rating=5" }} This is working fine if there is already some query data in the url. But if there is none then it is showing error.

Example: If my url is example.com/147?place=33 then it works but if my url is example.com/147 then it doesn't work.

I also tried, href={{ url()->full()."?rating=5" }}. This is working only in the opposite scenario.

How can I append my own query string after the full url regardless of the existence of current query?

Upvotes: 11

Views: 21412

Answers (2)

superfly
superfly

Reputation: 722

As suggested from @joão-henrique-silveira I add my comment as an answer for better visibility.

{{ request()->fullUrlWithQuery(['foo' => 'bar']) }} 

Upvotes: 41

apokryfos
apokryfos

Reputation: 40653

You can do something like:

href={{ url()->current().'?'.http_build_query(array_merge(request()->all(),['rating' => 5])) }}

This is just appending the extra parameter in the current request input and then rebuilding the query string.

Upvotes: 7

Related Questions