【发布时间】:2023-03-09 15:16:01
【问题描述】:
我有下面三个表。
+-----------------+ +---------------------+ +-------------+
| products | | product_countries | | countries |
+-----------------+ +---------------------+ +-------------+
| id | | id | | id |
| name | | product_id | | name |
| description | | country_code | | code |
+-----------------+ | other columns | +-------------+
+---------------------+
hasManyThrough 在 Country::class 模型中获取所有产品。
public function products()
{
return $this->hasManyThrough(
'App\Product',
'App\ProductCountry',
'product_id', // Foreign key on feeds table...
'id', // Foreign key on articles table...
'code', // Local key on users table...
'country_code' // Local key on feeds table...
);
}
我想要与国家或一个国家相关的产品:
$user = \App\Models\Country::find(1);
dd($user->products);
【问题讨论】:
-
我觉得你们的关系是many-to-many...