Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jmurphy1267's avatar

Append session variable to query string

Is there a way to append a query variable to each route? I need to addd a query parameter to each route if a flag is set in the users session.

So if the user has like USD flag on their session I would like for each route to have a query string like currency=USD

That way my vuejs app can get that variable and append it to the api calls that the vuejs components make.

0 likes
10 replies
jlrdw's avatar

A query string is passed anyway. Do you mean parameter. There is a big difference.

jmurphy1267's avatar

What I mean is if there a session variable like USD=1. Then when a call the route function I need to to append the value to the query string so

Session USD=1

all routes need to have currency=USD

for example

jlrdw's avatar

Do that in the controller or a query scope.

$query = Dog::where('dogname', 'like', $dogsch);
        if ($aval == "n") {
            $query->where('adopted', '=', 1);
        } else if ($aval == "y") {
            $query->where('adopted', '=', 0);
        }
        $dogs = $query->orderBy('lastedit', 'DESC')->paginate(5);

        $params = array('psch' => $dogsearch, 'aval' => $aval);

        return view('dog.index', compact('dogs', 'params'))

view:

echo '<td>' . $dogs->appends($params)->links() . '</td>';

Convert to blade as needed.

jmurphy1267's avatar

is there a way a can do it like this

Route::append('currency', Session::get('USD))

jlrdw's avatar

It's not done in route, it's done in controller:

$usd = Request::input('usd');

Or for session:

$usd = Session::get('usd');

Again is this a route parameter or a query string parameter, there is a difference.

You can use Session::get('usd') direct.

jmurphy1267's avatar

It is a query string parameter I need to have a certain query string parameter on every route regardless of what controller your currently in. Ideally I don't want to have to make sure i have that paremeter in the array of the route helper function in every controller or blade file

jmurphy1267's avatar

Yes it is but I that will not work when I have a frontend project and a backend project. WIth the frontend getting data from the backend with vuejs. and I have to pass that session variable to the api .

jlrdw's avatar

You can still get it into a variable fom session, like I showed above, then you can use json or whatever. You said session. The backend is where session variables are handled.

In a controller:

$usd = Session::get('usd');

Then do whatever you need to do with $usd.

Looks like question was modified.

rawilk's avatar

I would just create a config variable in your layout file, and then reference it that way. No need to deal with query strings.

In your layout

<script>
     window.config = {
           currency: '{{ session()->get('currency') }}' // Or however you determine what currency it is
     };
</script>

In your scripts

let currency = window.config.currency;

// do what you need with currency

Please or to participate in this conversation.