Your current implementation is functional, but there are a few areas where you can simplify or optimize the code. Here are some suggestions:
-
Use Eloquent Relationships: Instead of using raw queries, leverage Eloquent relationships to make your code more readable and maintainable.
-
Eager Loading: Use eager loading to reduce the number of queries executed.
-
Refactor the Query: You can refactor the query to make it more concise.
Here's a refactored version using Eloquent:
Models
First, ensure your models have the necessary relationships defined.
User.php
class User extends Model
{
public function games()
{
return $this->belongsToMany(Game::class)->withTimestamps();
}
public function points()
{
return $this->hasMany(Point::class);
}
}
Game.php
class Game extends Model
{
public function users()
{
return $this->belongsToMany(User::class)->withTimestamps();
}
}
Point.php
class Point extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function game()
{
return $this->belongsTo(Game::class);
}
}
Livewire Component
Now, refactor your Livewire component to use Eloquent:
public function render()
{
$scoreList = User::with(['games', 'points'])
->whereHas('roles', function ($query) {
$query->where('name', 'member');
})
->get()
->map(function ($user) {
$totalPoints = $user->points->sum('points');
$totalWins = $user->games->where('winner_id', $user->id)->count();
$totalCups = $user->games->where('cup_winner_id', $user->id)->count();
$totalGamesPlayed = $user->games->count();
return (object) [
'username' => $user->username,
'image' => $user->image,
'total_points' => $totalPoints,
'total_wins' => $totalWins,
'total_cups' => $totalCups,
'total_games_played' => $totalGamesPlayed,
];
});
return view('livewire.team', [
'scoreList' => $scoreList,
]);
}
Blade Template
Your Blade template can remain largely the same, as it is already well-structured. Just ensure it matches the data structure returned by the refactored code.
This refactoring leverages Eloquent's relationships and collection methods to make the code more readable and maintainable. It also reduces the complexity of the query logic by using Eloquent's built-in methods.