I have a User model that applies both for trainers and studenst. In my business logic Trainers have students assigned for a certain Unit, so i built a many-to-many relationship among them
public function students()
{
return $this->belongsToMany(User::class, 'trainer_student',
'trainer_id','student_id');
}
public function trainers()
{
return $this->belongsToMany(User::class, 'trainer_student','student_id', 'trainer_id');
}
Both in the User model. I checked the DB and there are 8 students related to a trainer for a certain Unit.
"trainer_student" Table Photo.
So i did this query with Tinker:
$trainer = User::findOrFail(21);
$trainer->students()->pluck('id');
But if i query like the usual way:
DB::table('trainer_student')->where('trainer_id',21)->pluck('student_id')
What is going on? Why the relationship shows less students? What am i doing wrong?
Thanks!