I have three models that should be linked in a pivot table: User, Student, Plan. Therefore, each user can subscribe to the plan.
What I have found so far is to create a support element for two models, for example User and Plan, and add student_id as an additional field:
$user->plans()->attach([1 => ['student_id' => $student_id]);
One of the problems is that if I try to get plans for a specific user, I will not get a student model, just an identifier, therefore:
return $this->BelongsToMany('App\Plan', 'plans_students_users', 'user_id', 'plan_id') ->withPivot('student_id');
So, I need to make a second request to get a student model.
Is there any other way to do this, given that I want to make queries in all directions, for example:
$user->plans() (attaching the students) $student->plans() (attaching the user) $plan->users() (attaching the students) $plan->students() (attaching the users)
eloquent laravel laravel-5
babbaggeii
source share