Laravel and pivot table to relate different model -
i'm wondering how, it's bit confusing.
i have fine belongs many relation between users , groups tables appropriate models of that.
but have table students, not users student students table maintain user_id field.
my question be: can use pivot table "group_user" relations between student , group model, in students table have "user_id" field? , how?
i tried
public function students() { return $this->belongstomany('student','group_user','group_id','user_id'); {
but don't see way how tell eloquent not take students.id take students.user_id???
assuming these relations:
subject belongsto group group belongstomany user user hasone student
you can this:
$subject = subject::find($someid); // of course multiple subject use eager loading: // $subjects = subject::with('group.users.student')->get(); $users = $subject->group->users; // related users foreach ($users $user) { $user->student; // null|student model }
Comments
Post a Comment