1 month ago

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

Posted 1 month ago by AbdulBazith

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>

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

Please sign in or create an account to participate in this conversation.