lots of improvements
This commit is contained in:
@@ -101,7 +101,77 @@ class GameController extends Controller
|
||||
|
||||
public function show(Game $game)
|
||||
{
|
||||
$game->load(['platforms', 'genres', 'screenshots', 'videos', 'similar_games', 'dlcs', 'reviews.user']);
|
||||
return view('games.show', compact('game'));
|
||||
// Load game relationships except reviews
|
||||
$game->load(['platforms', 'genres', 'screenshots', 'videos', 'similar_games', 'dlcs']);
|
||||
|
||||
// Load reviews with users separately (could be paginated if needed)
|
||||
$reviews = $game->reviews()->with('user')->get();
|
||||
|
||||
// Calculate rating distribution percentages
|
||||
$ratingDistribution = $this->calculateRatingDistribution($game);
|
||||
|
||||
return view('games.show', compact('game', 'ratingDistribution', 'reviews'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the rating distribution percentages for a game
|
||||
*
|
||||
* @param Game $game
|
||||
* @return array
|
||||
*/
|
||||
private function calculateRatingDistribution(Game $game)
|
||||
{
|
||||
$distribution = [];
|
||||
|
||||
// Get the total number of reviews for this game
|
||||
$totalReviews = $game->reviews()->count();
|
||||
|
||||
// If there are no reviews, return empty distribution
|
||||
if ($totalReviews === 0) {
|
||||
for ($i = 10; $i >= 1; $i--) {
|
||||
$distribution[] = [
|
||||
'range' => $i,
|
||||
'count' => 0,
|
||||
'percentage' => 0
|
||||
];
|
||||
}
|
||||
return $distribution;
|
||||
}
|
||||
|
||||
// Get the count of reviews for each rating range directly from the database
|
||||
$ratingCounts = $game->reviews()
|
||||
->selectRaw('
|
||||
CASE
|
||||
WHEN rating = 1 THEN 1
|
||||
WHEN rating = 2 THEN 2
|
||||
WHEN rating = 3 THEN 3
|
||||
WHEN rating = 4 THEN 4
|
||||
WHEN rating = 5 THEN 5
|
||||
WHEN rating = 6 THEN 6
|
||||
WHEN rating = 7 THEN 7
|
||||
WHEN rating = 8 THEN 8
|
||||
WHEN rating = 9 THEN 9
|
||||
WHEN rating = 10 THEN 10
|
||||
END as range_id,
|
||||
COUNT(*) as count
|
||||
')
|
||||
->groupBy('range_id')
|
||||
->orderBy('range_id', 'desc')
|
||||
->get()
|
||||
->keyBy('range_id');
|
||||
|
||||
// Build the distribution array with percentages
|
||||
for ($i = 10; $i >= 1; $i--) {
|
||||
$count = $ratingCounts->get($i)->count ?? 0;
|
||||
$percentage = ($count / $totalReviews) * 100;
|
||||
|
||||
$distribution[] = [
|
||||
'range' => $i,
|
||||
'count' => $count,
|
||||
'percentage' => $percentage
|
||||
];
|
||||
}
|
||||
|
||||
return $distribution;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user