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

AbdulBazith's avatar

groupBy in controller shows error Trying to get property 'employee' of non-object even though it has record

Guys iam working with a project,

I have small a issue in groupBy function

my model is EmpAttendance in my controller i did a coding

$empatts = EmpAttendance::where('hotel_id', Auth::user()->hotel_id)->get()->groupBy('emp_id');

it fetched records correctly but when i use my model name in my blade it shows error.

in my bade file i used like this

 @foreach($empatts as $empname => $results)
<td width=10%>{{ $loop->iteration}} </td>
<td width=10%>{{ $empname->employee->emp_name  }} </td>
@foreach

but this shows error Trying to get property 'employee' of non-object

my EmpAttendance model

 public function employee()
    {
        return $this->belongsTo('App\Employee', 'emp_id');
    }

my Employee model

 public function empattendance()
    {
        return $this->hasMany('App\EmpAttendance', 'emp_id');
    }

whats the problem??? temporarily i did like this get()->groupBy('employee.emp_name') this worked for me correctly, but in same name there may be many employees so the data mixed. kindly someone help please

0 likes
7 replies
tisuchi's avatar

@abdulbazith

As @sti3bas suggested, try this-

@foreach($empatts as $empname => $results)
    <td width=10%>{{ $loop->iteration}} </td>
    <td width=10%>{{ $results->employee->emp_name  }} </td>
@foreach
Sti3bas's avatar

@abdulbazith you are grouping a collection, so you will have nested array. Not sure if it was intended, if so, you might need another foreach:

@foreach($empatts as $empId => $results)
   @foreach($results as $employee)
      <td width=10%>{{ $loop->iteration}} </td>
      <td width=10%>{{ $employee->emp_name  }} </td>
   @foreach
@foreach

Otherwise move ->groupBy('emp_id') before ->get().

AbdulBazith's avatar

@sti3bas @tisuchi thank you for your responses

but

@foreach($empatts as $empname => $results)
    <td width=10%>{{ $loop->iteration}} </td>
    <td width=10%>{{ $results->employee->emp_name  }} </td>
@foreach

this shows the same error Property [employee] does not exist on this collection instance.

and i cant put groupBy before get like this ->groupBy('emp_id')->get(); that too shows error

SQLSTATE[42000]: Syntax error or access violation: 1055 'newsrp_04_01_2020.emp_attendances.id' isn't in GROUP BY (SQL: select * from `emp_attendances` where `hotel_id` = 1 and (`att_date` between 2019-12-01 and 2019-12-31) group by `emp_id`)

so what can i doo??

you said use another forloop. but iam already using 2 forloops

 @foreach($empatts as $empname => $results) //first forloop
 @php
 //these for storing total preasent and total absent values
 $totalpresent = 0;
 $totalabsent = 0;
 $aa = 0;
 $bb = 0;
 @endphp

 <tr>
     <td width=10%>{{ $loop->iteration}} </td> // displays serial number
    
     <td width=10%>{{ $empname}} </td>// displays emp id but need emp name
     @foreach($results as $result)  // second forloop

     @php
     $totalpresent += $result->att_count;  // adds the total of numbers to store present days
     @endphp
     @if($result->att_count == 0 ) // this if for having a count of 0's that is absent
     @php
     $aa += 1;
     @endphp

     @endif
     @if($result->att_count == 0.5 ) // this if for adding the total of 0.5's that is half days
     @php
     $bb += $result->att_count;  // 
     @endphp

     @endif

     @endforeach
     <td><b>{{$totalpresent}}</b></td>  // this displays the total preasent days
     <td><b></b>{{$aa + $bb }}</td>  // this displays the total absent by adding the count of 0's and total of 0.5's
     <td><b>{{$totalpresent + $aa + $bb}}</b></td> displays total days

 </tr>

 @endforeach



 how i have my att_count in table is ```0= absent 0.5, half day, 1 = present```


Kindly suggest your ideass please

AbdulBazith's avatar
AbdulBazith
OP
Best Answer
Level 5

@tisuchi @sti3bas thank you guys. i got the answer what i did is, just changed a line like below


@foreach($empatts as $empname => $results)
 @php

 $totalpresent = 0;
 $totalabsent = 0;
 $aa = 0;
 $bb = 0;
 @endphp

 <tr>
     <td width=10%>{{ $loop->iteration}} </td> 
    
     <td width=10%>{{ $empname}} </td>   // i removed this line from here and added this line after the for loop with a small change
// 


     @foreach($results as $result)  

     @php
     $totalpresent += $result->att_count;  
     @endphp
     @if($result->att_count == 0 ) 
     @php
     $aa += 1;
     @endphp

     @endif
     @if($result->att_count == 0.5 ) 
     @php
     $bb += $result->att_count;  
     @endphp

     @endif

     @endforeach

<td width=10%>{{ $results->employee->emp_name  }} </td>// i added here then it worked fine

     <td><b>{{$totalpresent}}</b></td>  // this displays the total preasent days
     <td><b></b>{{$aa + $bb }}</td>  // this displays the total absent by adding the count of 0's and total of 0.5's
     <td><b>{{$totalpresent + $aa + $bb}}</b></td> displays total days

 </tr>

 @endforeach 

Please or to participate in this conversation.