如何在Laravel中定义三对多关系的方式?

ant*_*ony 2 php mysql many-to-many laravel eloquent

我很难在Laravel 5中创建三个表/模型之间的三对多关系.到目前为止,从网上查找并没有给我任何可行的解决方案.

我有三个模型:用户,角色,论文.

我想要实现的是将三者相互连接起来.例如,用户可以与许多论文相关联,并且该用户可以在一个特定论文上具有许多不同的角色.

因此,用户可以在一个特定的论文上拥有许多角色(例如讲师和评论者),并且同一个用户也可以与另一个论文相关联,并且在该论文上也有另一个角色(例如,仅限教师).

到目前为止,我已经尝试了一种方法,这意味着创建另一个可以处理数据透视表的模型.所以我创建了一个模型关系.

我目前的表格:

用户

id 
name
Run Code Online (Sandbox Code Playgroud)

论文

id 
topic
Run Code Online (Sandbox Code Playgroud)

角色

id 
name
Run Code Online (Sandbox Code Playgroud)

关系

id
role_id (references id on roles) 
thesis_id (references id on theses)
user_id (references id on users)
Run Code Online (Sandbox Code Playgroud)

我现在的代码

user.php的

class User extends Authenticatable
{
    public function relations(){
        return $this->hasMany('App\Relation', 'relations');
    }
}
Run Code Online (Sandbox Code Playgroud)

Thesis.php

class Thesis extends Model
{
    public function relations(){
        return $this->hasMany('App\Relation', 'relations');
    }
}
Run Code Online (Sandbox Code Playgroud)

Role.php

class Role extends Model
{
    public function relations(){
        return $this->hasMany('App\Relation', 'relations');
    }
}
Run Code Online (Sandbox Code Playgroud)

Relation.php

class Relation extends Model
{
    public function user(){
    return $this->belongsToMany('App\User');
    }

    public function thesis(){
        return $this->belongsToMany('App\Thesis');
    }

    public function role(){
        return $this->belongsToMany('App\Role');
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我正在使用$ relation = new Relation; 插入数据透视表时的方法.

我不知道如何从数据透视表中获取数据.例如获得论文用户需要什么样的角色用户对特定论文.

我尝试过类似$ user-> thesis()$ user-> role()$ user-> thesis() - > role()但没有一个不起作用.我只得到一个未定义的方法错误.

我真的很感激,如果我能得到一些帮助或者至少某种方向,即使我在这里展示的内容有些正确.

谢谢.

sam*_*sam 5

您对通过中间表存储关系的多对多关系感兴趣.中间表可以在其上存储其他属性,因此,您可以创建thesis_users具有附加role_id属性的中间表.该文档涵盖了"检索中间表列"标题下的此用例.您不需要其他模型来表示这些关系.

pivot table如果需要确定Thesis用户具有哪些特定roles内容,您可以查询中间表(也称为a ),例如:

return $this->belongsToMany('App\Thesis')->wherePivot('role_id', 1);
Run Code Online (Sandbox Code Playgroud)

您的用户模型关系看起来像这样:

public function theses()
{
    return $this->belongsToMany('App\Thesis');
}
Run Code Online (Sandbox Code Playgroud)

你的论文模型关系看起来像这样:

public function users()
{
    return $this->belongsToMany('App\User');
}
Run Code Online (Sandbox Code Playgroud)

然后你可以在你的Thesis模型上做额外的关系约束,如下所示:

public function authors()
{
    return $this->belongsToMany('App\User')->wherePivot('role_id', 1);
}


public function reviewers()
{
    return $this->belongsToMany('App\User')->wherePivot('role_id', 2);
}


public function instructors()
{
    return $this->belongsToMany('App\User')->wherePivot('role_id', 3);
}
Run Code Online (Sandbox Code Playgroud)

在您的用户模型上,如下所示:

public function authored()
{
    return $this->belongsToMany('App\Thesis')->wherePivot('role_id', 1);
}

public function reviewer()
{
    return $this->belongsToMany('App\Thesis')->wherePivot('role_id', 2);
}
Run Code Online (Sandbox Code Playgroud)

等等

Thesis::find(1)->reviewers(); // returns every App\User with the role_id 2 on App\Thesis with ID 1
Thesis::find(1)->instructors(); // returns every App\User with the role_id 3 on App\Thesis with ID 1

User::find(1)->theses(); // returns each App\Thesis that the user has any role on 
User::find(1)->authored(); // returns each App\Thesis that the user has the author role on
Run Code Online (Sandbox Code Playgroud)