Laravel 的数据透视表 + 一般的数据透视表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46994355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel's pivot table + Pivot table in general
提问by Krystian Polska
What are laravel pivot tables and pivot tables in general? What is this all about?
什么是 Laravel 数据透视表和数据透视表?这是怎么回事?
Recently I made research about Pivot table. I thought I know them and What they are but then I probably was wrong about that.
最近我研究了数据透视表。我以为我知道它们以及它们是什么,但后来我可能错了。
I have always thought that a pivot table is just a table that is between two tables (Relation many to many)
我一直认为数据透视表只是两个表之间的表(关系多对多)
But then I started this research and It happened to be not that, but something like different architecture of normal table, where rows are columns. It's changed.
但是后来我开始了这项研究,结果恰好不是那样,而是类似于普通表的不同架构,其中行是列。变了。
But then Laravel's got pivot tables too. Started reading the documentation and doing research.Maybe I read wrong, but it looks like just pivot table in laravel - table in between two tables, many-to-many.
但是 Laravel 也有数据透视表。开始阅读文档并进行研究。也许我读错了,但它看起来只是 laravel 中的数据透视表 - 两个表之间的表,多对多。
Searching elsewhere but can't find proper information about it.
在别处搜索但找不到有关它的正确信息。
Okay, so be it. Laravel's pivot just many to many!
好吧,就这样吧。Laravel 的枢轴只是多对多!
Then I started project and Today I went to the point that making this in-between table as pivot drived me to an Issue, I had a problem with that... minutes and hours... couldn't fix that.
然后我开始了项目,今天我达到了将这个中间表作为枢轴驱动我到一个问题的地步,我遇到了一个问题......几分钟和几小时......无法解决这个问题。
Model was class Room_Student extends Pivot
型号是 class Room_Student extends Pivot
And what was the fix? Just changing it to class Room_Student extends Model
.
修复方法是什么?只需将其更改为class Room_Student extends Model
.
I don't think I understand pivot tables anymore and are they different types of pivots? Laravel's pivots are different?
我想我不再理解数据透视表了,它们是不同类型的数据透视表吗?Laravel 的pivots 不一样?
So my question is, what pivot tables really are? + Laravel's pivot tables. Are they different? What is this about?
所以我的问题是,数据透视表到底是什么?+ Laravel 的数据透视表。它们不同吗?这是关于什么的?
Please help me understand this.
请帮助我理解这一点。
回答by Harsha Jayamanna
When learning, focus only the pivot tables concept in Laravel (or eloquent). When I was learning I did not care about the general meaning of pivot table. I focused only on facts in the documentation (https://laravel.com/docs/5.5/eloquent-relationships#many-to-many)
学习时,只关注 Laravel(或 eloquent)中的数据透视表概念。当我学习时,我并不关心数据透视表的一般含义。我只关注文档中的事实(https://laravel.com/docs/5.5/eloquent-relationships#many-to-many)
many-to-many relationships require an additional table.And we can insert other useful data to this table as well. And can be used as a model in the system.
多对多关系需要一个额外的表。我们也可以在这个表中插入其他有用的数据。并且可以在系统中作为模型使用。
Example : User and Roles many-to-many relationship = User_roles
示例:用户和角色的多对多关系 = User_roles
Because of Pivot tables, you can retrieve intermediate table data as a model (like other models in the system).
由于数据透视表,您可以检索中间表数据作为模型(就像系统中的其他模型一样)。
Example:
例子:
//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;
}
NOTE: you can create a class by extending pivot. But you have to implement the correct relationships to make it work. Your code should look somewhat similar to below code.
注意:您可以通过扩展枢轴来创建一个类。但是您必须实施正确的关系才能使其发挥作用。您的代码应该看起来有点类似于下面的代码。
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
{
//
}
I hope this helps.
我希望这有帮助。
回答by Tschallacka
Simply put is a pivot table a table that joins two tables together
简单地说是一个数据透视表将两个表连接在一起的表
say you have a table users
说你有一个表用户
USERS:
user_id, user_name
say you have a table games
说你有桌游
GAMES
game_id, game_name
a user can play many games. games have many users playing them.
一个用户可以玩很多游戏。游戏有很多用户在玩。
To link them you make a third table
要链接它们,您需要制作第三张表格
GAMES_TO_USERS
game_id, user_id
with this table you can request which games a user plays, and which users play which game.
使用此表,您可以请求用户玩哪些游戏,以及哪些用户玩哪个游戏。
this table GAMES_TO_USERS
is in this case the pivot table.
GAMES_TO_USERS
在这种情况下,该表是数据透视表。
回答by Casper
If you know about many-to-many relationships this is common, to handle many-to-many relationships we use intermediate (pivot) table to store relationships of two tables. Ex: consider about “education” and “person” tables which are listed below
如果您了解多对多关系,这很常见,为了处理多对多关系,我们使用中间(枢轴)表来存储两个表的关系。例如:考虑下面列出的“教育”和“人”表
table: person
表:人
|------|-------|-----|
| id | name | age |
|------|-------|-----|
| 1 | Bob | 30 |
| 2 | John | 34 |
| 3 | Marta | 28 |
|------|-------|-----|
table: education
表:教育
|------|-------|
| id | level |
|------|-------|
| 1 | BSc |
| 2 | MSc |
| 3 | PhD |
|------|-------|
Think that Bob has BSc, MSc and John has BSc, MSc, PhD and Marta has BSc, now this is consider as many-to-many relationship and to sort this relationship you need to have intermediate table such as,
认为 Bob 有 BSc、MSc 和 John 有 BSc、MSc、PhD 和 Marta 有 BSc,现在这是考虑多对多关系,要对这种关系进行排序,您需要有中间表,例如,
table: person_education
表:person_education
|------------|--------------|
| person_id | education_id |
|------------|--------------|
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 1 |
| 2 | 3 |
| 3 | 1 |
|------------|--------------|
This table mainly stores the primary keys (IDs) of each relationship. This is the basic idea behind the pivot table and when you use larval there are some best practises such as,
该表主要存储每个关系的主键(ID)。这是数据透视表背后的基本思想,当您使用幼虫时,有一些最佳实践,例如,
- Name of the pivot table should consist of singular names of both tables, separated by undescore symbole and these names should be arranged in alphabetical order
- 数据透视表的名称应由两个表的单数名称组成,由下划线符号分隔,这些名称应按字母顺序排列
Laravel Ex:
Laravel 前:
Class Person extends Model {
public function education ()
{
return $this->belongsToMany('App\Education', 'person_education');
}
}
Moreover, you can specify the actual field names of that pivot table, if they are different than default person_id and education _id. Then just add two more parameters – first, the current model field, and then the field of the model being joined
此外,您可以指定该数据透视表的实际字段名称,如果它们与默认的 person_id 和教育 _id 不同。然后再添加两个参数——首先是当前模型字段,然后是被加入模型的字段
public function education() {
return $this->belongsToMany('App\Products', 'products_shops', 'shops_id', 'products_id');
}
回答by Jonjie
Keep this in mind
Pivot table— is a table used for connecting relationships between two tables.
请记住这一点
数据透视表— 是用于连接两个表之间关系的表。
Laravel part— laravel provides many-to-manyrelationship where you can use pivot table
, and it is very useful for many cases.
Laravel 部分——laravel 提供了多对多的关系,你可以在其中使用pivot table
,它在很多情况下都非常有用。
Example:
databases:users
, post_user
, posts
示例:
数据库:users
, post_user
,posts
User.php(Model)
User.php (模型)
class User extends Model{
public function posts(){
return $this->belongsToMany('Post');
}
}
Now, to access the all logged in user's posts:(view)
现在,访问所有登录用户的帖子:(查看)
@foreach(auth()->user()->posts as $post)
<li>{{ $post->name }}</li>
@endforeach
Relationship happened:
发生关系:
Remember we have post_user
table which is the pivot table we used. If we have:
请记住,我们有post_user
table,它是我们使用的数据透视表。如果我们有:
user_id
of 1
and we expect that it is the logged in user and post_id
of 1
, 2
, 3
, 4
, all this posts will be printed out like so:
user_id
of1
并且我们希望它是登录用户并且post_id
of 1
, 2
, 3
, 4
,所有这些帖子都将像这样打印出来:
|------------|--------------|
| post_id | user_id |
|------------|--------------|
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
|------------|--------------|
Output:
输出:
- PostName1
- PostName2
- PostName3
- PostName4
- 职位名称1
- 职位名称2
- 职位名称3
- 职位名称4