2 回答
TA贡献1813条经验 获得超2个赞
你好,你能测试一下吗:
$mostRatedRestaurants = Restaurant::App\Menu::find(1);
$mostRatedRestaurants->where('status', 1)->get();
问候
TA贡献1898条经验 获得超8个赞
解决了,谢谢各位。
我是创建方法
public function getAvgRate($mostRatedRestaurants){
foreach ($mostRatedRestaurants as $mostRatedRestaurant){
$ratedRestaurant = 0;
$countMenuHasRate = 0;
$avgRateRestaurant = 0;
foreach($mostRatedRestaurant->menus as $menu){
// عدد تقييمات الوجبة
$countReviewForMenu = count($menu->reviews);
$rate = 0;
foreach($menu->reviews as $reviews){
$rate += $reviews->rate;
}
if(count($menu->reviews) !== 0){
$rateForMenu = round($rate/$countReviewForMenu); // rate Menu;
$countMenuHasRate += 1;
$ratedRestaurant += $rateForMenu;
}
}
if($ratedRestaurant !== 0) {
$avgRateRestaurant = round($ratedRestaurant / $countMenuHasRate);
}
$mostRatedRestaurant['rate'] = $avgRateRestaurant ?? 0;
}
return $mostRatedRestaurants;
} // End Method getAvgRate
和内部方法索引
$mostRatedRestaurants = Restaurant::where('status', 1)->with('profileImage', 'menus')->get();
$mostRatedRestaurants = $this->getAvgRate($mostRatedRestaurants); // Add Rate
$mostRatedRestaurants = $mostRatedRestaurants->sortByDesc('rate')->take('9'); // Sort Rate
return view('user.content.restaurant.index', compact('mostRatedRestaurants'));
在刀片中 用于回波率
@php
$rate = 5 - $mostRatedRestaurant->rate;
@endphp
@for ($i = 1; $i <= $rate; $i++)
<li class="far fa-star text-secondary float-left text-left"></li>
@endfor
@php
$rate = $mostRatedRestaurant->rate;
@endphp
@for ($i = 1; $i <= $rate; $i++)
<li class="fa fa-star"></li>
@endfor
你可以改进代码再次感谢。
- 2 回答
- 0 关注
- 85 浏览
添加回答
举报