如何在 Laravel 中实现 BelongsToThrough 关系?
我有一个表:
**projects_table**
id
**categories_table**
id
project_id
**properties_table**
id
category_id
类别属于项目
public function project(){
return $this->belongsTo('App\Project');
}
属性属于类别。
public function category(){
return $this->belongsTo('App\Category');
}
如何建立关联?
属性通过类别属于项目。
public function project(){
return $this->belongsToThrough('App\Project', 'App\Category');
}
编辑问题。
因为我想在我的应用程序上实现多租户,而租户是项目。我想通过项目将类别和属性分开。类别属于项目, 属性属于类别。
因此,我希望通过类别在属性和项目之间建立关系。 目前,我需要在类别和属性表上都添加
project_id
,但我认为这不是正确的方法。
belongsToThrough
关系 https://github.com/staudenmeir/belongs-to-through - Patrick