我有像下面的 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();