V
V
vanek_parenek2022-03-14 15:37:45
Laravel
vanek_parenek, 2022-03-14 15:37:45

How to add a new parameter to an existing query string?

How can I add new parameters to a query string that already has some parameters.
Those. Let's say that my current url is site?status=1 , and I need to click on the link, for example, to get the string site?status=1&color=2 . What do I need to write in the href attribute to get this result? I tried request()->fullUrlWithQuery(), but then it turns out that I have to specify a parameter and a value in the method, but if I have, for example, 10 parameters, it turns out that I need to specify each one.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey delphinpro, 2022-03-14
@vanek_parenek

I tried request()->fullUrlWithQuery(), but then it turns out that I have to specify a parameter and a value in the method, but if I have, for example, 10 parameters, it turns out that I need to specify each one.

What for? Only the one added by this link
https://laravel.com/docs/9.x/requests#retrieving-t...
If you would like to append query string data to the current URL, you may call the fullUrlWithQuery method. This method merges the given array of query string variables with the current query string:

<a href="{{ request()->fullUrlWithQuery(['status' => 1]) }}">Status</a>
<a href="{{ request()->fullUrlWithQuery(['color' => 1]) }}">Color</a>

P
pLavrenov, 2022-03-14
@pLavrenov

function.parse-str.php

A
Anton Anton, 2022-03-14
@Fragster

this is how it works (welcome is the name of the route in the template of which this code is):

<div><a href="{{route('welcome', array_merge($_GET, ['a' => '1']))}}">a</a></div>
<div><a href="{{route('welcome', array_merge($_GET, ['b' => '1']))}}">b</a></div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question