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

thelazzyriveryogi's avatar

Session as a temporary pointer for UI

I am currently using Session::put as a way to set the current portion of a users data that they are interacting with. So for examply if a user is part of 10 teams I am setting Session::put('that team') any time they click to a different team. The rest of the website then looks for the currently set team and generates the forms and links etc based on the currently put team.

A few considerations off the top of my head... Session is or is not generally used for extremely temporary sets of data? I know by its nature session is good for the short term single use scenario of a trip to a website... but should it be set constantly any time a user shifts focus on the site? as I believe it is necessary in this circumstance? or iis there a maybe better way?

another thing I wonder which i havent had to do yet is test this. I suppose I can jus set the session variable in the test as well... how would I be affected if the application is not using http?

0 likes
1 reply
jbloomstrom's avatar

Sounds like a good use for caching. https://laravel.com/docs/5.4/cache

Make sure to include the user id or some other way to identify the user as part of the cache key. Something like:

Cache::forever( "selectedTeam.{$user->id}", $the_team);

That way the user can sign out and back in or sign in from a different device and have the same experience. If you don't want to cache it forever, you can specify a TTL.

Cache::remember("selectedTeam.{$user->id}", $ttl_in_minutes, $the_team);

Please or to participate in this conversation.