Пока сделал так, но не нравится:
$prices_min = $this->prices()->limit(1)->orderBy('days')->first();
if($days < $prices_min) {
return $prices_min;
}
$exact_match = $this->prices()->where('days','=',$days)->first();
if($exact_match) {
return $exact_match;
}
$smaller = $this->prices()->where('days','<',$days)->first();
if($smaller) {
return $smaller;
}
throw new NotFoundException('Not Found');
Либо чуть покороче:
$prices_min = $this->prices()->limit(1)->orderBy('days')->first();
if($days < $prices_min) {
$price = $prices_min;
}
$exact_match = $this->prices()->where('days','=',$days)->orWhere('days','<',$days)->orderByDesc('days')->first();
if($exact_match) {
$price = $exact_match;
}
Но кажется можно получше сделать.