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模式中 –
我覺得沒有辦法輕鬆做到。 –