skip to Main Content

I have this mechanism of checking the logged-in user role in a blade page so that I can give him/ her the selected pages but I find after a user has logged in and stayed idle for a long time and when he is back and wants to continue in the page he is getting an error

ErrorException
Trying to get property ‘id’ of non-object (View: /var/www/html/poss_v1/resources/views/dashboard/student/index.blade.php)

and my code is here below

<?PHP
$user = Auth::user();  
$user_id = $user->id;    //----here is the problem it is missing the id when the session has ended--//
$role_user = AppModelsRoleUser::where('user_id', $user_id)->first();
$role_name = AppModelsRole::where('id', $role_user->role_id)->first();
$role = $role_name->name;
?>

3

Answers


  1. To increase the lifetime of your session, you may modify the env variable SESSION_LIFETIME to whatever minutes you wish.

    And to make sure your user doesn’t get this error, you may check if the session is still alive with Auth::check()

    Login or Signup to reply.
  2. In Laravel blade file you should check logged in user in this way.

    @php
    if(Auth::check()){
    $user = Auth::user();  
    $user_id = $user->id;
    $role_user = AppModelsRoleUser::where('user_id', $user_id)->first();
    $role_name = AppModelsRole::where('id', $role_user->role_id)->first();
    $role = $role_name->name;
    }
    @endphp
    
    Login or Signup to reply.
  3. @php
        if(Auth::check()){
            $role_user_id = AppModelsRoleUser::where('user_id', auth()->id())->first()->role_id;
            $role = AppModelsRole::where('id', $role_user_id)->first()->name;
        }
    @endphp
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search