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

noblemfd's avatar

How to Apply left join in Laravel Query

In my Laravel-5.8, I have these models:

  1. hr_employees

  2. hr_departments

  3. hr_results

    class HrEmployee extends Model
    {
       protected $table = 'hr_employees';
    
       protected $primaryKey = 'id';
    
       protected $fillable = [
                  'id',
                  'hr_status',
                 'company_id',
                 'email',
                 'type_code',
                'first_name',
                 'other_name',
                 'last_name',
                 'department_id',
             ];
    
     public function results()
     { 
           return $this->hasMany('App\Models\HrResult', 'employee_id', 'id'); 
     }  
    
     public function department()
     {
       return $this->belongsTo('App\Models\HrDepartment','department_id','id');
     }        
    }
    
    
    class HrDepartment extends Model
    {
    
       protected $table = 'hr_departments';
    
       protected $fillable = [
                   'id',
                   'dept_name',
           'dept_head',	
               ];
    
      public function depthead()
      {
         return $this->belongsTo('App\Models\Hr\HrEmployee','dept_head','id');
      } 
    
     }
    
    
    class Result extends Model
    {
      protected $table = 'appraisal_goals';
    
      protected $fillable = [
                 'id',
         'employee_id',	
                 'is_published',
                 'is_approved',
             ]; 
    
     public function employee()
     {
       return $this->belongsTo('App\Models\Hr\HrEmployee','employee_id','id');
     }    
    }
    

hr_employees: I want to select all the fields from hr_employees where hr_status=0, where company_id=$company and where type_code IN (1,2,5). Then concatenated first_name, other_name and last_name as name.

hr_departments: I want to select dept_name and dept_head from hr_departments

results: I want to group hr_results by employee_id where is_published = $published. Select is_approved (0=Processing, 1=Approved, 2=Rejected).

NOTE:

An employee can only belong to one department. Each department will have only one departmental head.

All the employee will have department, but not all the employee will have results.

If any employee does not have result, it should make is_approved = 'Not Available'

Controller

 public function employee_results()
{
    $company = Auth::user()->company_id;
    $published = DB::table('result_identity')->select('id')->where('company_id', $company)->where('is_current', 1)->pluck('id');

      $employeeresults = ...


    return view('employee_results')
              ->with('employeeresults', $employeeresults);
}

And then render everything on a view using

Any help on how to complete the code in the Controller via the Query Builder or the LEFT JOIN?

Kindly assist.

Thanks

0 likes
1 reply

Please or to participate in this conversation.