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

noblemfd's avatar

How to do Leave count per department

I am using Laravel-5.8 to get the count of employees that have applied for leave and those that have not applied for a particular year.

I have these 3 tables: hr_employees, hr_departments and hr_leave_requests.

class HrDepartment extends Model
{
  protected $table = 'hr_departments';
  protected $fillable = [
	  'id',
      'company_id',
      'dept_name',
    ];
}

class HrEmployee extends Model
{
  protected $table = 'hr_employees';
  protected $fillable = [
	  'id',
      'company_id',
      'first_name',
      'last_name',
      'department_id',
      ];
  public function department()
  {
      return $this->belongsTo('App\Models\Hr\HrDepartment','department_id','id');
  }  
}

class HrLeaveRequest extends Model
{
  protected $table = 'hr_leave_requests';
  protected $fillable = [
	  'id',
      'company_id',
      'leave_status',
      'employee_id',
    ];
  public function department()
  {
      return $this->belongsTo('App\Models\Hr\HrDepartment','department_id','id');
  }  
}

An employee can apply for a leave several times in a year, but it will be counted as one. A department has many employees. Here is my code below:

 $leaveReports = DB::table('hr_departments AS d')
    ->leftJoin('hr_employees AS e', function($join) use ($userCompany)
    {
     $join->on('d.id', '=', 'e.department_id')
     ->where('e.company_id', '=', $userCompany)
     ->where('e.hr_status', '=', '0');
    })
    ->join('hr_leave_requests AS lr', function($join) use ($userCompany)
    {
     $join->on('e.id', '=', 'lr.employee_id')
     ->where('lr.company_id', '=', $userCompany)
     ->where('lr.leave_status', '!=', '0');
     })
     ->where('d.company_id', '=', $userCompany)
     ->select(
        'd.dept_name',
         DB::raw('COUNT("lr.id") as applied_count'),
         )
     ->groupby('lr.employee_id')
     ->get();

I want to display the result below:

https://i.stack.imgur.com/xBQmw.png

I want to list all the departments, count the number of employees that have applied for leave and those that have not. if leave_status is not 0, then employee_id has applied for leave. To get applied in a department, subtract total applied in that department from total employee in that department.

If I have 3 departments and 50 employees. It shows all the departments and show the count those that have applied and those not not applied per department

However, instead of get the type of result in the diagram, it calculated all the employees as total applied.

How do I resolve this?

Thanks

0 likes
1 reply
CorvS's avatar

@noblemfd If I understand you correctly you could simply use withCount:

HrDepartment::query()
    ->withCount(['employees', 'leaveRequests' => fn($q) => $q->where('leave_status', '!=', 0)])
    ->get();

For that to work you would have to define the hasMany relationships (employees and leaveRequests) on your Department model tho.

After that you have the total number of employees and those who applied for leave, the rest is a simple substraction.

https://laravel.com/docs/8.x/eloquent-relationships#relationship-counting-and-custom-select-statements

Please or to participate in this conversation.