I have 4 tables (users,teachers,courses,course_teacher)
so I have a foreign key in teachers table references id on users table, and course_teacher as pivot table (many to many relationship)
users table has (id,firstname,lastname,email,password)
teachers table has (id,users_id,about)
courses table has (id,name)
and I have these relations in the models:
// User.php
public function teacher() {
return $this->hasMany(Teacher::class);
}
// Teacher.php
public function course() {
return $this->belongsToMany(Course::class);
}
public function user() {
return $this->belongsTo(User::class);
}
// Course.php
public function teacher() {
return $this->belongsToMany(Teacher::class);
}
in my database I have data in each of these three tables and I attached teacher and course together in the pivot table
in tinker I tried this:
$course = App\Course::find(1);
// I got here some data
$teacher = App\Teacher::find(1);
$course->teacher;
// I got (id,users_id,about)
$teacher->user;
// I got his data from users table
but when I tried to get the name and email of the teacher :
$course->teacher->user;
it gives me this error:
Exception with message 'Property [user] does not exist on this collection instance.'
how can I get this teacher data as user ?