【发布时间】:2017-07-08 00:40:12
【问题描述】:
例如,我有 4 张桌子:
建筑物
id、标题、...
房间
id, building_id, title, ...
公司
id、标题、...
companies_to_buildings
company_id, building_id
我想从房间模型中获得公司。
从建筑模型我可以做类似的事情
class Building extends Model
{
public function companies(){
return $this->belongsToMany('App\Company', 'company_building');
}
}
或
class Building extends Model
{
public function companies(){
return $this->belongsToMany('App\Company', 'company_building', 'building_id', 'company_id');
}
}
但如果我在房间模型中执行此操作,它将无法正常工作。它将尝试在company_to_buildings 表中查找company_to_buildings.building_id=room.id 的行。如何从房间模型中获得公司?
【问题讨论】:
标签: php eloquent many-to-many laravel-5.1