【发布时间】:2018-01-24 09:10:02
【问题描述】:
我有两个模型,一个是报价(用于优惠表),另一个是品牌(用于品牌表)。这些模型之间存在“多对多”的关系。
我有一个表 offer_brands 来映射这两个表之间的关系。在这个表中,我有将这个表链接到 offer 表的 offer_id 列和将这个表链接到brands 表的brand 列usind brand_name 列在brands 表中。
在品牌模型中,我想要一个返回所有报价的关系。
我知道我可以做到
public function offers()
{
return $this->belongsToMany(Offer::class, 'offer_brands', 'offer_id', 'brand_id'); // But don't have brand_id column insted I have brand column that contains brand names
}
我也试过了
public function offers()
{
return $this->belongsToMany(Offer::class, 'offer_brands', 'offer_id', 'brand'); // But Eloquent compare this column with id column of brands table.
}
【问题讨论】:
标签: mysql laravel eloquent laravel-eloquent