子模型的载荷计数关系

时间:2020-07-12 10:28:27

标签: laravel eloquent

我建立了模型,并试图检索与特定模型相关的模型的数量,但是我使用模型的方式出现的问题却使我返回了所有关系的数量。

以下是我的案例的描述,我有那些模型:

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加载DocumentCategory之间的关系计数,我尝试了以下操作,但是它加载了SubCategory和{{ 1}}:

Document

0 个答案:

没有答案