简体   繁体   English

雄辩的Laravel-如何在laravel中加入3表?

[英]eloquent-Laravel-How to join 3 table in laravel?

How can I get some data from 3 table using eloquent in laravel 5? 如何在Laravel 5中使用雄辩的方法从3表中获取一些数据? I have 3 table, Team, Boards, User 我有3个表格,团队,委员会,用户

Team Model: 团队模式:

public function member()
{
    return $this->belongsToMany(User::class)
                ->withPivot('status','user_role')
                ->withTimestamps();
}


public function boards()
{
    return $this->belongsToMany(Boards::class)->withTimestamps();;
}

Boards Model 板子型号

public function member()
{
    return $this->belongsToMany(User::class)
                ->withPivot('status','user_role')
                ->withTimestamps();
}

public function team()
{
    return $this->belongsToMany(Team::class)
                ->withTimestamps();
}

Table structure : 表结构:

Team 球队

id
team_name

Team_User (Pivot table) Team_User(数据透视表)

id
user_id
team_id

Boards 主板

id
board_name

Boards_user (pivot Table) Boards_user(数据透视表)

id
boards_id
user_id

The boards are belongs to team, and i want to get the data of boards that belong to a team (ex : team A) that assign to a user (ex: user1) but also member of team A. How can i accomplish that? 董事会属于团队,我想获取属于一个团队(例如:A团队)的董事会数据,这些数据既分配给用户(例如:user1),又属于A团队的成员。我该如何做到这一点?

According to your lastest comments: 根据您的最新评论:

  • You don't need to relate User and Board directly. 您不需要直接联系UserBoard This can be accessed from the many-to-many relationship from User m----m Team . 可以从User m----m Team 的多对多关系中进行访问。
  • A Board belongs to a single Team, a Team can have many boards. 董事会属于一个团队,一个团队可以拥有多个董事会。 This is a one-to-many relationship: Team m-----1 Board 这是一对多的关系: Team m-----1 Board

So your tables should be structured this way: 因此,您的表应采用以下结构:

users
=====
    - id    
    - ...

teams
=====
    - id
    - team_name

team_user
=========
    - id
    - team_id
    - user_id
    - status
    - user_role

boards
======
    - id
    - team_id
    - board_name

Then your relationships: 然后你的关系:

User.php user.php的

public function teams()
{
    return $this
       ->belongToMany(Team::class)
       ->withPivot('status','user_role');
}

Team.php Team.php

public function members()
{
    return $this
       ->belongToMany(User::class)
       ->withPivot('status','user_role');
}

public function boards()
{
    return $this->hasMany(Board::class);
}

Board.php Board.php

public function team()
{
    return $this->belongsTo(Team::class);
}

So now you can access the boards related to a User specific team doing this: 因此,您现在可以访问与特定于用户的团队相关的板,执行此操作:

$user_boards = User
                 ::find(1) // getting a User
                 ->teams() // accessing the Teams relationship
                 ->where('team_name', '=', 'My Team Name') // Constraining
                 ->first() // accessing the Team record
                 ->boards // getting the boards.

Additionally to that, you could add a shortcut in the User model to access the boards: 除此之外,您可以在用户模型中添加快捷方式来访问板:

User.php user.php的

...

public function boardsOfTeam($teamName)
{
    return $this
       ->teams()
       ->where('team_name', '=', $teamName)
       ->first()
       ->boards;
}

Then in your controller (or wherever you want): 然后在您的控制器中(或您想要的任何地方):

$user_boards = User::find(1)->boardsOfTeam('My Team Name');

This is totally optional, of course. 当然,这完全是可选的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM