简体   繁体   中英

How to define three table relationship in models in laravel 5.6

In my project there are three table like (teacher, attendance,user) and i have define the relationship in there model. in teacher table relation with both attendance and user but user has no relationship with attendance when i want to fetch data it show an error like Property [attendance_status] does not exist on this collection instance . how can i solve this problems.

This is User model

public function teacher() {
    return $this->hasOne('App\Teacher');
}

this is Teacher model

public function attendance() {
    return $this->hasMany(Attendance::class,'teacher_id','user_id');
}

this is attendance model

public function teacher() {
   return $this->belongsTo(Teacher::class,'teacher_id','user_id');
}

and in my attendance controller I define like this

$staffAttendances = Teacher::with(['attendance','user'])->get();

foreach ($staffAttendances as $attd) {          
      echo $attd->user->first_name .'<br>';
      echo $attd->designation.'<br>';
      echo $attd->attendance->attendance_status.'<br>';
}

尝试

 $attd->attendance[0]->attendance_status

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM