noblemfd
1 month ago
123
1
Laravel

HOD and Line Manager to See only staff attached to them

Posted 1 month ago by noblemfd

I have this model for employee:

class HrEmployee extends Model { public $timestamps = false;

protected $table = 'hr_employees';

protected $fillable = [
              'employee_code',
              'user_id',
              'address',
              'company_id',
              'email',
              'line_manager_id',
              'employee_designation_id',
              'employee_job_title_id',
              'employee_status_id',
              'employee_type_id',
              'employement_type_id',
              'employment_date',
              'first_name',
              'last_name',
          'is_hod'
          ];
protected $casts = [];

public function user()
{
    return $this->belongsTo('App\User');
}

public function parent()
{
    return $this->belongsTo('HrEmployee', 'ine_manager_id');
}

public function children()
{
    return $this->hasMany('HrEmployee', 'ine_manager_id');
}

public function company()
{
    return $this->belongsTo('App\Models\Organization\OrgCompany','company_id');
}


public function department()
{
    return $this->belongsTo('App\Models\Organization\OrgDepartment','department_id');
}

}

and controller

public function index()
{
    $userCompany = Auth::user()->company_id;
    abort_unless(\Gate::allows('employee_access'), 403);
    if (auth()->user()->isAdmin()) {
        $employees = HrEmployee::all();
    } else {
        $employees = HrEmployee::where('company_id', $userCompany)->get();
    }
    return view('hr.employees.index')->with('holidays', $holidays);
}

Already I have set it that if logged in user is Admin, he should view all. If else view his company staff only.

But, I also want the HOD to view only the employee in his department and Line manager to view only the employee that have him as line manager.

  1. For the HOD, if is_hod is 1, he should view all the employee that have his department_id

  2. For line manager, it should check all the employee that have the line_manager_id. The line_manager_id is derived from id in employee table.

How do I achieve this from the controller.

Thank you

Please sign in or create an account to participate in this conversation.