If I understood correctly - if there's a discount_id, you want discounted price, if not - the normal price.
If you didn't know, there's an inbound whereBetween method for laravel's query builder, which makes it a bit easier ^^
$query->whereHas('price', function($q) use ($min, $max){
$q->where(function($q) use ($min, $max){
$q->has('discount')->whereBetween('category_discounted_price', [$min, $max])
})->orWhere(function($q) use ($min, $max){
$q->doesntHave('discount')->whereBetween('price', [$min, $max]);
});
})
P.S. Not tested, but should produce the needed query.