首页 文章

Laravel的数据透视表Pivot表一般

提问于
浏览
13

一般来说,laravel枢轴表和数据透视表是什么?这是怎么回事?

最近我研究了Pivot表 . 我以为我知道他们和他们是什么但是那时我可能错了 .

我一直认为数据透视表只是两个表之间的表(关系多对多)

但后来我开始进行这项研究,碰巧不是那样,而是像普通表的不同架构,行是列 . 它改变了 .

但是Laravel也得到了数据透视表 . 开始阅读文档并进行研究 . 也许我读错了,但看起来只是在laravel中的数据透视表 - 两个表之间的表,多对多 .

搜索其他地方但无法找到有关它的正确信息 .

好的,就这样吧 . Laravel的支点只有很多人!

然后我开始了项目,今天我开始着手制作这个中间 table 作为枢轴把我带到一个问题,我有一个问题......分钟和小时......无法解决这个问题 .

型号是 class Room_Student extends Pivot

什么是修复?只需将其更改为 class Room_Student extends Model .

我认为我不再了解数据透视表,它们是不同类型的枢轴吗? Laravel的支点不同?

所以我的问题是,透视表到底是什么? Laravel的支点表 . 他们不一样吗?这是关于什么的?

请帮我理解这个 .

4 回答

  • 3

    学习时,只关注Laravel(或雄辩)中的数据透视表概念 . 当我在学习时,我并不关心数据透视表的一般含义 . 我只关注文档中的事实(https://laravel.com/docs/5.5/eloquent-relationships#many-to-many

    多对多关系需要一个额外的表 . 我们也可以向该表插入其他有用的数据 . 并且可以用作系统中的模型 .

    示例:用户和角色多对多关系= User_roles

    enter image description here

    由于数据透视表,您可以将中间表数据检索为模型(与系统中的其他模型一样) .

    例:

    //get user by id
    $user = App\User::find(1);
    
    //get roles of this user
    foreach ($user->roles as $role) {
    
      //pivot attribute returns a model which represent user_role table
      echo $role->pivot->created_at;
    
    }
    

    注意:您可以通过扩展数据透视来创建一个类 . 但是你必须实现正确的关系才能使它工作 . 您的代码看起来应该与下面的代码类似 .

    class Student extends Model
    {
        /**
         * The users that belong to the role.
         */
        public function Rooms()
        {
            return $this->belongsToMany('App\Room')->using('App\Room_Student');
        }
    }
    
    class Room extends Model
    {
        /**
         * The users that belong to the role.
         */
        public function Students()
        {
            return $this->belongsToMany('App\Student')->using('App\Room_Student');
        }
    }
    
    class Room_Student extends Pivot
    {
        //
    }
    

    我希望这有帮助 .

  • 14

    简单地说就是一个数据透视表,它将一个表连接在一起

    说你有一个表用户

    USERS:
    user_id, user_name
    

    说你有桌上游戏

    GAMES
    game_id, game_name
    

    用户可以玩很多游戏 . 游戏有很多用户在玩它们 .

    要链接它们,您需要制作第三个表格

    GAMES_TO_USERS
    game_id, user_id
    

    使用此表,您可以请求用户玩哪些游戏,以及哪些用户玩哪个游戏 .

    此表 GAMES_TO_USERS 在这种情况下是数据透视表 .

  • 2

    如果你知道多对多关系这很常见,为了处理多对多关系,我们使用中间(数据透视)表来存储两个表的关系 . 例如:考虑下面列出的“教育”和“人”表

    table :人

    |------|-------|-----|
    |  id  | name  | age |
    |------|-------|-----|
    |  1   | Bob   | 30  |
    |  2   | John  | 34  |
    |  3   | Marta | 28  |
    |------|-------|-----|
    

    表:教育

    |------|-------|
    |  id  | level |
    |------|-------|
    |  1   | BSc   |
    |  2   | MSc   |
    |  3   | PhD   |
    |------|-------|
    

    认为Bob有BSc,MSc和John有BSc,MSc,PhD和Marta有BSc,现在这被认为是多对多的关系并且要排序这种关系你需要有中间表,如,

    表:person_education

    |------------|--------------|
    |  person_id | education_id |
    |------------|--------------|
    |  1         |    1         |
    |  1         |    2         |
    |  2         |    1         |
    |  2         |    1         |
    |  2         |    3         |
    |  3         |    1         |
    |------------|--------------|
    

    该表主要存储每个关系的主键(ID) . 这是数据透视表背后的基本思想,当您使用larval时,有一些最佳实践,例如,

    • 数据透视表的名称应由两个表的单数名称组成,由非核心符号分隔,这些名称应按字母顺序排列

    Laravel Ex:

    Class Person extends Model {
        public function education ()
        {   
          return $this->belongsToMany('App\Education', 'person_education');
        }
    }
    

    此外,如果它们与默认的person_id和education _id不同,您可以指定该数据透视表的实际字段名称 . 然后再添加两个参数 - 首先是当前模型字段,然后是要连接的模型的字段

    public function education() { 
      return $this->belongsToMany('App\Products', 'products_shops', 'shops_id', 'products_id'); 
    }
    
  • 2

    记住这一点

    Pivot table - 是用于连接两个表之间关系的表 .

    Laravel part - laravel提供many-to-many关系,您可以使用 pivot table ,它在很多情况下非常有用 .

    Example:
    数据库: userspost_userposts

    User.php (型号)

    class User extends Model{
    
      public function posts(){
         return $this->belongsToMany('Post');
      }
    
    }
    

    Now, to access the all logged in user's posts: (查看)

    @foreach(auth()->user()->posts as $post)
      <li>{{ $post->name }}</li>
    @endforeach
    

    Relationship happened:

    请记住,我们有 post_user 表,它是我们使用的数据透视表 . 如果我们有:

    user_id1 并且我们希望它是登录用户和 1234post_id ,所有这些帖子都会打印出来,如下所示:

    |------------|--------------|
    |  post_id   |   user_id    |
    |------------|--------------|
    |  1         |    1         |
    |  2         |    1         |
    |  3         |    1         |
    |  4         |    1         |
    |------------|--------------|
    

    Output:

    • PostName1

    • PostName2

    • PostName3

    • PostName4

相关问题