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

noblemfd's avatar

How to display page tab based on User Role

I have a Laravel-5.8 application whereby a user can have more than one role. I am using Laravel Spatie.

Controller

public function index()
{

try {  
    if(Auth::user()->hasRole('HR Admin - General'))
    {
      $leaverequesthrs = HrLeaveRequest::where('line_manager_id', $userEmployee)->where('company_id', $userCompany)->whereYear('created_at', date('Y'))->get();       
    }
    if(Auth::user()->hasRole('HOD')){
      $leaverequesthods = HrLeaveRequest::where('line_manager_id', $userEmployee)->where('company_id', $userCompany)->whereYear('created_at', date('Y'))->get();

    }
    if(Auth::user()->hasRole('Line Manager')){    
      $leaverequests = HrLeaveRequest::where('line_manager_id', $userEmployee)->where('company_id', $userCompany)->whereYear('created_at', date('Y'))->get();

    }   
     $incompleteCount = $leaverequests->filter(function($item) {
          return ($item->leave_status == 1 || $item->leave_status == 2 || $item->leave_status == 3 || $item->leave_status == 4);
      })->count(); 

     $incompletehrCount = $leaverequesthrs->filter(function($item) {
          return ($item->leave_status == 1 || $item->leave_status == 2 || $item->leave_status == 3 || $item->leave_status == 4);
      })->count(); 

    $incompletehodCount = $leaverequesthods->filter(function($item) {
          return ($item->leave_status == 1 || $item->leave_status == 2 || $item->leave_status == 3 || $item->leave_status == 4);
      })->count(); 


      return view('hr.employee_leaves.index')
      ->with(['leaverequesthrs' => $leaverequesthrs, 'incompletehrCount' => $incompletehrCount])
      ->with(['leaverequesthods' => $leaverequesthods, 'incompletehodCount' => $incompletehodCount])
      ->with(['leaverequests' => $leaverequests, 'incompleteCount' => $incompleteCount]);

    } catch (Exception $exception) {
        Session::flash('error', 'Action failed! Please try again');
        return back();
        }      
}

In the Application, I have three roles: HOD, Line Manager and HR Admin - General. I want each tab to be rendered and visible based on user's role

view

<div class="card">
          <div class="card-header p-2">
            <ul class="nav nav-pills">
              <li class="nav-item"><a class="nav-link active" href="#manager" data-toggle="tab">Line Manager Review</a></li>
              <li class="nav-item"><a class="nav-link" href="#hod" data-toggle="tab">HOD Review</a></li>
              <li class="nav-item"><a class="nav-link" href="#hrservice" data-toggle="tab">HR Service Review</a></li>
            </ul>
          </div> 
          <div class="card-body">
            <div class="tab-content">

              <div class="active tab-pane" id="manager">
                <div class="row">
                    <div class="table-responsive">
        <table class=" table table-bordered table-striped table-hover datatable">
            <thead>
                <tr>

                    <th width="10%">
                        #
                    </th>
                    <th>
                        Leave Type
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach($leaverequests as $key => $leaverequest)
                        <td>
                            {{$key+1}}
                        </td>
                        <td>
                            {{isset($leaverequest->leavetype) ? $leaverequest->leavetype->leave_type_name : 'N/A'}}
                        </td>
                @endforeach 
            </tbody>
        </table>
                    </div>

                </div>    
             </div> 

            <div class="tab-pane" id="hod">
                <div class="row">
                    <div class="table-responsive">
        <table class=" table table-bordered table-striped table-hover datatable">
            <thead>
                <tr>

                    <th width="10%">
                        #
                    </th>
                    <th>
                        Leave Type
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach($leaverequesthods as $key => $leaverequesthod)
                        <td>
                            {{$key+1}}
                        </td>
                        <td>
                            {{isset($leaverequesthod->leavetype) ? $leaverequesthod->leavetype->leave_type_name : 'N/A'}}
                        </td>                                     
                </tr>
                @endforeach 
            </tbody>
        </table>
                    </div>

                </div>     
            </div>      

            <div class="tab-pane" id="hrservice">
                <div class="row">
                    <div class="table-responsive">
        <table class=" table table-bordered table-striped table-hover datatable">
            <thead>
                <tr>

                    <th width="10%">
                        #
                    </th>
                    <th>
                        Leave Type
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach($leaverequesthrs as $key => $leaverequesthr)
                        <td>
                            {{$key+1}}
                        </td>
                        <td>
                            {{isset($leaverequesthr->leavetype) ? $leaverequesthr->leavetype->leave_type_name : 'N/A'}}
                        </td>
                @endforeach 
            </tbody>
        </table>
                    </div>

                </div>     
            </div>                                        

            </div> 
        </div>           
</div>

When I tried to render the page, it didn't load. I t remained at the previous page I had loaded (not this particular page). So when I

dd($exception->getMessage());

I got this error:

undefined variable leaverequesthod.

I discovered that its because the logged in user is not having the role HOD.

However, a user don't need to have all the roles. The page should only render the tab based on the role of logged in user.

How do I resolve this issue?

Thank you.

0 likes
0 replies

Please or to participate in this conversation.