9

I'm using get parameters for search filters, and need to be able to change a value of the query string variable and return the modified url (as a string variable, nothing fancy, no redirect or anything). This is what I have so far and what is happening:

public function index(Request $request){

    echo $request->fullUrl();
    // outputs https://test.com/search?type=somestring

    $request->merge(['type' => 'anotherstring']);

    echo $request->fullUrl();
    // still outputs https://test.com/search?type=somestring

    // is there a way to change a parameter value in the url and
    // return the modified url string?

}

I figure if worse comes to worse, I'll just parse the string manually, but it feels like there's a "laravel way" to go about this that I happen to be missing?

2
  • 1
    To change the URL in the browser address bar you either need to redirect or use JavaScript. There's no way around it. Commented Jun 5, 2017 at 18:41
  • I don't want to change the url in the address bar, I simply want to modify the url string and save it as a variable Commented Jun 5, 2017 at 18:42

3 Answers 3

27

Use fullUrlWithQuery instead.

echo $request->fullUrlWithQuery(['type' => 'anotherstring']);
Sign up to request clarification or add additional context in comments.

1 Comment

If you use URL::forceScheme('https'), in your app service provider, this will not generate https urls, you will have to use url()->full()
0

Worse case you can do:

 echo url()->current().(!empty($request->all())?"?":"").http_build_query($request->all());

Comments

0

if you need to keep some previous parameter and replace/add others:

$request->fullUrlWithQuery(array_merge($request->all(),['overwritten'=>'changed_value','added'=>'new_value']));

it replaces the query with a merge of previous parameters, adding and overwriting with the new parameters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.