i have a simple where query that repeats in a foreach for some times that can be a lot really so here is my query :
for ($i = 0; $i < count($hasdate); $i++) {
$roomprice = RoomPricingHistory::
Where('accommodation_room_id', $hasroom[$i])
->where('from_date', '<=', $hasdate[$i])
->where('to_date', '>=', $hasdate[$i])
->get()->sortBy('created_at');
$lastget = last($roomprice);
$last_price = last($lastget);
if ($last_price) {
$final_price[] = $last_price->sales_price;
} else {
$has_not_capacity = $hasdate[$i];
}
}
so each time this runs it takes a bout 2,509.10ms in telescope and here is what the telescope shows me as the query which is running on table
select
*
from
`room_pricing_histories`
where
`accommodation_room_id` = 3
and `from_date` <= "2019-06-01 09:00:00"
and `to_date` >= "2019-06-01 09:00:00"
so any idea on how to optimize this query ??
$hasdate?