@abdulbazith that's because $empname is a key, not an object foreach (array_expression as $key => $value).
https://www.php.net/manual/en/control-structures.foreach.php
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
@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.