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

noblemfd's avatar

Syntax error or access violation: 1055 'laravelapp.ltd.no_of_days' isn't in GROUP BY

In my Laravel-5.8 project, I am developing employee leave application. In the application, in a particular year, each employee can apply for different leaves. when an employee logs in I want to display a table as shown below that shows Leave Balance of each leave type name of the logged in user

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

I have 3 tables that are applicable

class HrLeaveType extends Model
{
   protected $table = hr_'leave_types';

   protected $fillable = [
              'id',
              'leave_type_name',
          ];

   public function leavetypedetail()
   {
       return $this->hasMany('App\Models\LeaveTypeDetail');
   }  
}

class HrLeaveTypeDetail extends Model
{
   protected $table = 'hr_leave_type_details';
   protected $fillable = [
              'id',
              'leave_type_id',
              'employment_type_id',
              'no_of_days',
          ];

   public function leavetype()
   {
       return $this->belongsTo('App\Models\HrLeaveType', 'leave_type_id', 'id');
   }

   public function employeetype()
   {
       return $this->belongsTo('App\Models\HrEmployeeType', 'employee_type_id', 'id' );
   }    
}

class HrLeaveRequest extends Model
{
   protected $table = 'hr_leave_requests';
   protected $fillable = [
              'id',
              'employee_id',
              'leave_type_id',
              'leave_status',
              'no_of_days',
          ];


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

   public function leavetype()
   {
       return $this->belongsTo('App\Models\HrLeaveType','leave_category_id');
   }
}

As earlier said, the expected result is to have 4 columns (Leave Category , Applicable Leave, Approved Leave , Available )

Controller

public function leave_balance()
{
    $userId = Auth::user()->id;
$userCompany = Auth::user()->company_id;
    $leaveBalance = DB::table('hr_leave_types AS lt')
                ->leftJoin('hr_leave_type_details AS ltd', function($join) use ($userCompany)
                    {
                        $join->on('ltd.leave_type_id', '=', 'lt.id')
                        ->where('ltd.company_id', '=', $userCompany);
                    })
                ->leftJoin('hr_leave_requests AS lr', function($join) use ($userCompany, $userEmployee)
                    {
                        $join->on('lr.leave_type_id', '=', 'lt.id')
                        ->where('lr.company_id', '=', $userCompany)
                        ->where('lr.employee_id', '=', $userEmployee)
                        ->whereYear('lr.commencement_date', '=', date('Y'))
                        ->where('lr.leave_status', 4);
                    })                        
                ->leftJoin('hr_employees AS e', function($join) use ($userCompany, $userEmployee)
                    {
                        $join->on('e.id', '=', 'lr.employee_id')
                        ->where('e.company_id', '=', $userCompany)
                        ->where('e.id', '=', $userEmployee)
                        ->where('e.employee_type_id', '=', 'ltd.employee_type_id');
                    })  
                ->where('lt.company_id', '=', $userCompany)
                ->select(
                        'lt.leave_type_name as leaveCategory',
                        'ltd.no_of_days as applicableLeave',
                        DB::raw("IFNULL(SUM(lr.no_of_days),0) as approvedLeave")
                       )
                  ->groupBy('lt.leave_type_name', 'e.id')
                ->get();                    

return view('leave-balances')
	->with('leaveBalance', $leaveBalance);
}

available = applicableLeave - approvedLeave

I want to achieve this result

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

When I tried to run the controller, I got this error:

#message: "SQLSTATE[42000]: Syntax error or access violation: 1055 'laravelapp.ltd.no_of_days' isn't in GROUP BY (SQL: select lt.leave_type_name as leaveCategory, `l ▶"

There is no way I can group by 'no_of_days'

I only want to group by leave_type_name and employee_id (e.id)

How do I resolve this?

Thanks

0 likes
1 reply

Please or to participate in this conversation.