Laravel:HasmanyThrough的关系,以获得许多到许多表值



我有像下面的 three table关系

国家 ->(有许多)省 ->(有许多)城市

我想知道有什么办法雄辩地获得所有第二个孩子的国家,即城市有status=active

我知道,通过雄辩提供的hasManyThrough关系助手找到一个县的活跃城市。但是我需要检索all Countries who even has a single active city

注意:我不想在每个国家/地区循环。

使用 whereHas()

Country::whereHas('cities', function($query) {
    $query->where('status', 'active');
})->get();

最新更新