简体   繁体   English

Laravel 5-查询数据库中的多个关联表

[英]Laravel 5 - Query Multiple Associative Tables in Database

Description: 描述:


I'm working on a database setup that's requiring that I use multiple associative tables and I want to know how associative tables work within Laravel while taking advantage of the Eloquent Relationships. 我正在做一个数据库设置,要求我使用多个关联表,并且我想知道在利用雄辩性关系的同时Laravel中关联表如何工作。

I have a dataset containing soccer games. 我有一个包含足球比赛的数据集。 I need to store players, games, and specific player information for a game. 我需要存储玩家,游戏和游戏的特定玩家信息。 So my structure should look something like: 因此,我的结构应类似于:

 Player Table: +------------+---------+ | id | integer | +------------+---------+ | name | string | +------------+---------+ | team | string | +------------+---------+ | country | string | +------------+---------+ | average | string | +------------+---------+ | updated_at | Date | +------------+---------+ | created_at | Date | +------------+---------+ Game Table +--------------+---------+ | id | integer | +--------------+---------+ | name | string | +--------------+---------+ | country | string | +--------------+---------+ | tournament | string | +--------------+---------+ | score | string | +--------------+---------+ | started_at | Date | +--------------+---------+ | ended_at | Date | +--------------+---------+ | updated_at | Date | +--------------+---------+ | published_at | Date | +--------------+---------+ 

The issue being that I need a table that associates the two together as a player can have many games and a game has many players . 问题是我需要一个将两者关联在一起的表,因为一个玩家可以玩很多游戏,而一个游戏 可以玩 很多玩家


Question: 题:


How would I go about structuring my code? 我将如何构造代码?

  • Do I write a player_game model? 我要写一个player_game模型吗?
  • Do I store associations in a different way than usual because this is Laravel? 因为这是Laravel,我是否以不同于通常的方式存储关联?

Intuitively I would want to write a player_game migration that contains game specific information for a player. 直观地讲,我想编写一个player_game迁移,其中包含玩家的游戏特定信息。 But how does that work with Laravel 5 and the hasMany() attributes? 但这如何与Laravel 5和hasMany()属性一起使用?

Follow your intuition . 跟随你的直觉 You were right, what you are looking for is called a Many to Many Relationship . 您说对了,您要寻找的被称为“ 多对多关系”

Create the intermediate table and then you can play with different queries, if Laravel HasMany type methods doesn't fill your needs, you can always query the intermediate table directly. 创建中间表,然后您可以使用不同的查询,如果Laravel HasMany类型方法不能满足您的需求,则始终可以直接查询中间表。

you need to create a pivot table named game_player many to many relationship . 您需要创建一个名为game_player 多对多关系数据透视表。 you can store your game specific information inside this table. 您可以将游戏特定信息存储在此表中。

relationship in Game Model, 游戏模型中的关系

public funtion players()
{ 
    return $this->hasMany('Player');
}

to access the game specific information from the pivot table, 从数据透视表访问游戏特定信息,

foreach ($game->player as $player)
{
    echo $player->pivot->created_at;
}

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

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