Laravel的三向多对多关系 [英] Three-way many-to-many relationship in Laravel

查看:105
本文介绍了Laravel的三向多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个模型需要在数据透视表中相关:用户,学生,计划。所以每个用户都可以订阅一个学生的计划。



目前为止,我发现的是为两个模型创建一个枢纽,如用户和计划,以及将student_id附加为一个额外的字段:

  $ user-> plans() - > attach([1 => ['student_id'=> $ student_id]); 

一个问题是,如果我尝试检索特定用户的计划,得到学生模型,只是id,所以:

  return $ this-> BelongsToMany('App \Plan' ,'plans_students_users','user_id','plan_id')
- > withPivot('student_id');

所以,我必须做一个第二个查询来获得学生模型。



有没有其他的方法去做,因为我想在各个方向进行查询,例如:

  $ user-> plans()(附加学生)
$ student-> plans()(附加用户)
$ plan->用户()(附加学生)
$ plan-> students()(附加用户)


解决方案

我经常使用另一个模型来抽象三向多对多关系。



我们有我们的关系调用关系关系



db 结构:

 表关系:id,user_id,student_id,plan_id 

该应用有以下四种型号:




  • 用户

  • 学生

  • 计划

  • 关系



这是我们如何使用关系连接四个模型:



用户,计划,学生:

  function relations(){
return $ this-> hasMany(Relation :: class);
}

关系:

  function student(){
return $ this-> belongsToMany(Student :: class);
}

function user(){
return $ this-> belongsToMany(User :: class);
}

函数plan(){
return $ this-> belongsToMany(Plan :: class);
}

您可以检索这样的实体:

  //获取与用户相关的学生计划
$ user-> relations() - > where('student_id',$学生) - >首先() - >计划();

//从关系中获取所有实体
foreach($ user->关系为$ relation){
$ plan = $ relation->计划;
$ student = $ relation-> student;
}

它是我一直在开发的唯一解决方案Laravel。


I have three models that need to be related in a pivot table: User, Student, Plan. So each user can subscribe a student to a plan.

What I've found so far is to create a pivot for two of the models, say User and Plan, and attach the student_id as an extra field:

$user->plans()->attach([1 => ['student_id' => $student_id]);

One problem with this is that if I try to retrieve the plans for a particular user, I don't get the student model, just the id, so:

return $this->BelongsToMany('App\Plan', 'plans_students_users', 'user_id', 'plan_id')
->withPivot('student_id');

So, I'd have to do a second query to get the student model.

Is there any other way to go about it, given that I'll want to make queries in all directions, e.g:

$user->plans() (attaching the students)
$student->plans() (attaching the user)
$plan->users() (attaching the students)
$plan->students() (attaching the users)

解决方案

I often use another model to abstract a three-way many to many relations.

We have our relation I will call the relation relation.

The db structure:

table relations: id, user_id, student_id, plan_id

The app has the following four models:

  • User
  • Student
  • Plan
  • Relation

Here is how we connect the four models using Relationships:

User, Plan, Student:

function relations() {
   return $this->hasMany(Relation::class);
}

Relation:

function student() {
   return $this->belongsToMany(Student::class);
}

function user() {
   return $this->belongsToMany(User::class);
}

function plan() {
   return $this->belongsToMany(Plan::class);
}

You can retrieve the entities like this:

//get the plan of a student related to the user
$user->relations()->where('student_id', $student)->first()->plan();

//get all entities from the relation
foreach ($user->relations as $relation) {
    $plan = $relation->plan;
    $student = $relation->student;
}

Its the only solution I have found in all the time I have been developing on Laravel.

这篇关于Laravel的三向多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆