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

h3nsen's avatar

Change session save on click

Hi everyone,

my name is Julia and I'm new in this board and also new to Laravel but until now I love it :)

But at the moment I got a problem which google cant answer me. I have a array with some user specific ids. As an example a user has ids 3,4,5 and 6. They come from a database.

Than they are stored in an accessibale array. With this array I build navigation menu in main.blade.php.

So every user is able to see a some pages with some data. These data comes also from a database. This user should be able to show only the data from id 3,4,5 and 6. So I want to store the actual id in a session variable.

And the user should be able to change this session id with a link in the navigation menu. BUT he also should be able to change it to 3,4,5 or 6. So not to 7 because this would be an other users data.

So how can i savely change the session variable by an link in the menu? Afterwards I could also check if the new variable which should be stored in the session varaible is element of users id (in this case 3 to 6).

Thanks for any advice :)

0 likes
8 replies
h3nsen's avatar

Hi JackJones,

thanks for your answer :) but I know how to set session and validate them. Thats not the problem. Perhaps I wasnt explicit enough.

My problem is how can I change the actual session variable by a clickable link>

So I need that user can click on a link than the session variable is change from 4 to (e.g.) 6.

For security reasons I would validate it afterwards by validation.

Thanks and best Julia

JackJones's avatar

I assumed because the channel was set to Laravel that the issue was Laravel

If I understand you correctly, and you don't want the user to leave the page, you would need to send an ajax request to a Controller when the number is pressed, which leads to a Controller that updates the session

1 like
h3nsen's avatar

Yes right i am using Laravel :)

Yes but it also would be possible to refresh the page, if a link is pressed.

But how can i change the session by a link or by pressing a number? It wouldnt be a problem to reload the page so AJAX would be fine but isnt necessary..

Thanks in advance :)

JackJones's avatar

You would load a very simple Controller, validate the request (the id variable or whatever you decide to call it, update the session and return back();

$this->validate($request, [
        'number' => 'required|integer|in:' . implode(',', 'your valid numbers')
    ]);

session()->put('number', $request->input('number'));

return back();
1 like
h3nsen's avatar

Thanks a lot, you are my hero ;) I think this is the solution.

One more question:

If i create the Controller like above. How can I call it. Because there is no view rendered and the controller should be accsessable from different pages with different view.

JackJones's avatar
Level 14

Literally any way you like

Route::get('/change-number', [ 'as' => 'change-number',
                        'uses' => 'ChangeNumberController@update'
                        ]);

{!! route('change-number') !!}

You could do it with wildcards which would also be an option, but this method is the most basic

1 like
h3nsen's avatar

OK it works! Thanks again hundred times! And have a nice evening :)

Ah and with the validation function there is no chance to manipulate this thing, right?

Please or to participate in this conversation.