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

behnampmdg3's avatar

Is it better to store details in a session or retrieve on each page load?

Is it better to store details in a session on login... or retrieve on each page load?

User data, course description etc

0 likes
4 replies
behnampmdg3's avatar

@SIANGBOON - Not much change. My concern is how much data is too much for a session variable. For example

$all_courses = \DB::table('courses')
                        ->where('user_id', '=', $request->business_id)               
                        ->select('courses.title','courses.id','courses.description')
                        ->orderBy('courses.title')
                        ->get();
            $request->session()->put('all_courses', $all_courses); 

Do this once and since courses don't change that often it should be fine.

siangboon's avatar

Did you really watch the video?? if not i suggest you to watch once.

By default, session driver is file, mean all the session values will store in file so I don't think the data size is the big issue for your app until the app hit million users access at once...

https://laravel.com/docs/5.8/session

In your case, I may consider caching as it's not user specific and not much change.

martinbean's avatar

@behnampmdg3 If you’re worried about retrieving data on each page load then I’d suggest looking at caching, not “where” to store things.

If you’re thinking about caching, then I’d suggest looking at data and see if the database queries you’re making are heavy and could be re-written to be more performance, or whether you’re just thinking about caching for caching’s sake.

Premature optimization is the root of all evil

Please or to participate in this conversation.