我有以下范围:
public function scopeFilterByAmenities($query, AmenityFilters $amenities)
{
$ids = DB::table('property_amenities')->distinct()
->where(function($query) use ($amenities){
$query->whereIn('amenity_id', $amenities->ids_enabled());
$query->where('status', $amenities->enabled_value);
})
->orWhere(function($query) use ($amenities){
$query->whereIn('amenity_id', $amenities->ids_disabled());
$query->where('status', $amenities->disabled_value);
})
->groupBy('property_id')
->havingRaw('count(*) = '.$amenities->count()) // sum of $amenities->ids_enabled() and $amenities->ids_disabled()
->pluck('property_id');
return $query->whereIn('id', $ids);
}
使用Laravel ORM返回相同的结果不应该是更简单的方法吗?我已经设法以这种方式做了但从我的角度看起来很混乱,Laravel也有内置的东西所以可以让事情更清楚吗?