What data should be stored in a session variable in Laravel 5

I am new to laravel. I am working on a laravel 5 application with different types of users. I need information about what type of user is currently registered in different sections of my views:

I'm currently doing something like below on various controller methods and with a user object, I can determine what type of user it is in my opinion:

In the controller:

public function someMethod(){
    $user = Auth::user();
    return view('applications.show', compact('user'));
}

In view:

if($user->is_manager)
   // do this
else if($user->is_admin)
  // do that 

Since I need information about the type of user in different views, I called Auth::user()in several places, and I'm starting to think that this adds some load to the database. Is it better to store a custom type in a session variable and what data should I store in my session?

0
2

, .

is_manager User - ...

public function is_manager()
{
    // Check if the session has been set first.
    if(\Session::has('is_manager')) {
        return \Session::get('is_manager');
    }

    // Do your necessary logic to determine if the user is a manager, ex...
    $is_manager = $this->roles()->where('name', '=', 'manager')->count() == 1;

    // Drop it in the session
    \Session::put('is_manager', $is_manager);

    return $is_manager;
}

, , , , .

+1

:

  • , , , , , . , , , .
  • .
  • Redis. - , .
0

All Articles