Laravel使用with()方法将where子句应用于belongsToMany查询

Laravel使用with()方法将where子句应用于belongsToMany查询,laravel,relationship,Laravel,Relationship,我有一个带有品牌标识的产品表,还有一个带有、产品标识和类别标识的类别产品表 我的产品型号: class Product extends Model { public function categories() { return $this->belongsToMany(Category::class)->withTimestamps(); } } 我的品牌型号: class Brand extends Model { public function produ

我有一个带有品牌标识的产品表,还有一个带有、产品标识和类别标识的类别产品表

我的产品型号:

class Product extends Model
{
  public function categories() {
    return $this->belongsToMany(Category::class)->withTimestamps();
  }
}
我的品牌型号:

class Brand extends Model
{
   public function products() {
     return $this->hasMany(Product::class)->with(['categories']);
   }
}
我的问题是,如何从属于特定类别的品牌实例中获取产品

$brand = App\Brand::find(1);

/* 
  I want to be able to do something in the likes of :
*/

 $brand->products_which_are_under_this_category
使用['categories']方法从产品内部删除,并编写类似的查询

$brand->products()->with(['categories' => function ($query) {
    $query->where('category_id', CATEGORY_ID);
}])->get();
希望这能有所帮助