我建立了模型,并试图检索与特定模型相关的模型的数量,但是我使用模型的方式出现的问题却使我返回了所有关系的数量。
以下是我的案例的描述,我有那些模型:
Category
模型
public function subcategories()
{
return $this->belongsToMany('App\SubCategory');
}
/**
* Get the documents for the category
*/
public function documents()
{
return $this->hasMany('App\Document');
}
SubCategory
模型
public function categories()
{
return $this->belongsToMany('App\Category');
}
/**
* Get the documents for the subcategory
*/
public function documents()
{
return $this->hasMany('App\Document');
}
我只想为选定的Subcategory
加载Document
和Category
之间的关系计数,我尝试了以下操作,但是它加载了SubCategory
和{{ 1}}:
Document