0
我有一个这样的数据库。Laravel Eloquent如何将多对多和一对多关系与3个表结合
+----------------+
| Tables_in_test |
+----------------+
| a_b |
| a_c |
| as |
| bs |
| cs |
+----------------+
bs和cs表与表格有多对多的关系。所以a_b表和a_c表是数据透视表。
这是为表
+----+------+
| id | name |
+----+------+
| 1 | A1 |
| 2 | A2 |
+----+------+
这是BS表
+----+------+
| id | name |
+----+------+
| 1 | B1 |
+----+------+
这是CS表
+----+------+------+
| id | b_id | name |
+----+------+------+
| 1 | 1 | C1 |
| 2 | 1 | C2 |
+----+------+------+
这是A_B透视表
+------+------+
| a_id | b_id |
+------+------+
| 1 | 1 |
+------+------+
这是a_c数据透视表。
+------+------+
| a_id | c_id |
+------+------+
| 1 | 1 |
| 2 | 2 |
+------+------+
这是我的A模型。
class A extends Model
{
protected $table = "as";
public function b(){
return $this->belongsToMany("App\B");
}
public function c(){
return $this->belongsToMany("App\C");
}
}
这是BS表
class B extends Model
{
protected $table = "bs";
public function c(){
return $this->hasMany("App\C");
}
}
B型我只是想为相关的表来查询Ç表值。
我尝试这个查询
A::where("id",1)->with("b.c")->get();
但是,这一结果也给了我这是在“为”表与A2 C2的值。 “为”表中,我只想得到仅与A1值相关的C1值。
我该怎么做?感谢您的帮助
谢谢你的回答。但是C模式必须收集到一对多的B模式中 –
我觉得没有办法轻松做到。 –