У меня есть приложение Laravel -5.8, в котором пользователь может иметь более одной роли. Я использую Laravel Spat ie.
Контроллер
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();
}
}
В приложении у меня есть три роли: HOD, Line Manager и HR Admin - General. Я хочу, чтобы каждая вкладка отображалась и отображалась в зависимости от роли пользователя
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>
Когда я пытался отобразить страницу, она не загружалась. Я остался на предыдущей странице, которую я загрузил (не на этой конкретной странице). Поэтому, когда я
dd ($ exception-> getMessage ());
я получил эту ошибку:
неопределенная переменная оставить запрос.
Я обнаружил, что это потому, что вошедший в систему пользователь не имеет роли HOD.
Однако пользователю не нужно иметь все роли. На странице должна отображаться только вкладка в зависимости от роли зарегистрированного пользователя.
Как мне решить эту проблему?
Спасибо.