I am trying to store values to array using foreach loop, here is my code
public function priceRegulation(){
$prices = Price::where('hotel_id', $this->id);
$lowerPrices = [];
if($prices->count() > 0){
foreach ($prices as $price) {
$lowerPrices[] = $price;
}
if(sizeof($lowerPrices) < 1){
return false;
} else{
return true;
}
} else{
return 'empty';
}
}
I have also tried setting $lowerPrices to 0 (int type) and increment it through foreach, but result was the same variable remaind at 0. I have tested if statements and they work fine. The problem is with foreach it basically does nothing. I am using Laravel Framework. Any help is welcome! Regards
CodePudding user response:
In this line:
$prices = Price::where('hotel_id', $this->id);
You aren't fetching the data, change it to:
$prices = Price::where('hotel_id', $this->id)->get();
CodePudding user response:
$prices = Price::where('hotel_id', $this->id)->get();
Do this you will get the data.
CodePudding user response:
public function priceRegulation(){
$prices = Price::where('hotel_id', $this->id)->get();
$lowerPrices = [];
// dd($prices); check if your $prices is not 0
if($prices->count() > 0){
foreach ($prices as $price) {
array_push($lowerPrices, $price);
}
if(sizeof($lowerPrices) < 1){
return false;
} else{
return true;
}
} else{
return 'empty';
}
}