mikefolu
mikefolu

Reputation: 1333

Laravel - How to Get List of Employees not in the Leave table

I have this two tables/models:

class HrEmployee extends Model
{
    protected $table = 'hr_employees';

    protected $fillable = [
                  'first_name',
                  'last_name',
                  'employee_code',
              ];


    public function leaverequest(){
        return $this->hasMany('App\Models\Hr\HrLeaveRequest');
    }  
}



class HrLeaveRequest extends Model
{

    protected $table = 'hr_leave_requests';

    protected $primaryKey = 'id';

    protected $fillable = [
                  'id',
                  'employee_id',
                  'department_id',
              ];


    public function employee()
    {
        return $this->belongsTo('App\Models\Hr\HrEmployee','employee_id');
    }


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

}

Using the controller function below, how do I use Eloquent Query to

  1. get the List of Employees and their departments (first_name, last_name, employee_code, department_name) who are in the hr_leave_requests (HrLeaveRequest) table

  2. count the Employees and their departments (first_name, last_name, employee_code, department_name) who are in the hr_leave_requests (HrLeaveRequest) table

       public function leave_request_approve()
       {
          $employee_not_in_list          = HrLeaveRequest...;
    
     }
    

Thank you

Upvotes: 0

Views: 231

Answers (1)

Mohammad Mirzaee
Mohammad Mirzaee

Reputation: 111

You can use Eager Loading and Querying Relationship Existence like below :

HrEmployee::with('leaverequest')->has('leaverequest')->get();

Upvotes: 1

Related Questions