Laravel - 属于关系的条件

Vin*_*aux 3 laravel eloquent

我使用 Laravel 5.6,我有 3 个模型:

  • 区域
  • 店铺
  • 商人

该地区有很多商店

public function stores()
{
    return $this->hasMany('Beproxy\App\Models\Store');
}
Run Code Online (Sandbox Code Playgroud)

商店属于一个商家

public function merchant()
{
    return $this->belongsTo('Beproxy\App\Models\Merchant');
}
Run Code Online (Sandbox Code Playgroud)

在我的Merchant中,我有一个 tinyInt 来定义该 Merchant 是否处于活动状态(状态

我可以在区域模型中编写一个新函数来获取属于活跃商家的所有商店吗?

通过hasMany关系,我可以使用:

public function storesWithProducts()
{
    return $this->hasMany('App\Models\Store')->has('products');
}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何使用belongsTo的条件,我在我的Area中尝试过,但它加载了所有内容:

public function storesWithMerchantActive()
{
    return $this->hasMany('App\Models\Store')
        ->with(['merchant' => function($query) {
            $query->where('state', '=', 1);
        }])
        ->has('products');
}
Run Code Online (Sandbox Code Playgroud)