Laravel的数据透视表+数据透视表 [英] Laravel's pivot table + Pivot table in general

查看:151
本文介绍了Laravel的数据透视表+数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What are laravel pivot tables and pivot tables in general? What is this all about?

最近,我对数据透视表进行了研究.我以为我知道他们以及他们是什么,但是那时我可能是错的.

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.

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

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.

正在其他地方搜索,但找不到适当的信息.

Searching elsewhere but can't find proper information about it.

好的,就这样吧. Laravel的枢轴对许多人而言!

Okay, so be it. Laravel's pivot just many to many!

然后我开始了项目,今天我要说的是,把这张中间表作为枢轴将我带到一个问题上,我有一个问题……几分钟和几个小时……无法解决.

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.

模型为class Room_Student extends Pivot

解决方法是什么?只需将其更改为class Room_Student extends Model.

And what was the fix? Just changing it to class Room_Student extends Model.

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

I don't think I understand pivot tables anymore and are they different types of pivots? Laravel's pivots are different?

所以我的问题是,数据透视表到底是什么? + Laravel的数据透视表.他们不同吗?这是怎么回事?

So my question is, what pivot tables really are? + Laravel's pivot tables. Are they different? What is this about?

请帮助我理解这一点.

推荐答案

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

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)

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

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.

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

Example : User and Roles many-to-many relationship = User_roles

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

Because of Pivot tables, you can retrieve intermediate table data as a model (like other models in the system).

示例:

//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
{
    //
}

我希望这会有所帮助.

这篇关于Laravel的数据透视表+数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆