简体   繁体   English

多对多关系查询

[英]Many to many to many relationship query

I got 3 main table. 我有3张主桌。

Table A Table B Table C 表A表B表C

Table A has many to many relationships with Table B so they have a pivot table ab 表A与表B有许多关系,因此它们具有数据透视表ab

Table B has also many to many relationships with Table C so they also have a pivot table bc 表B与表C也有许多关系,因此它们也有数据透视表bc

So because of this Table A has many to many through, relationship with Table C. 因此,由于此表A与表C具有多对多关系。

So what is the MySQL query if I wanted to get all rows of Table C that is related to Table A? 那么,如果我想获取与表A相关的表C的所有行,那么MySQL查询是什么?

You work backwards: 您向后工作:

$tableAID = 1;

TableC::whereHas('tableB', function($q) use ($tableAID) {
    $q->whereHas('tableA', function($q) use ($tableAID) {
        $q->where('id', $tableAID);
    });
})->get();

If you set the models well, A->Bs->Cs; 如果您设置好模型,则A->Bs->Cs; should do the trick. 应该可以。

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

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