I'm looking to do something like this, but with Eloquent: get latest record for each ID
Personally, I have three tables: Game, Assignments, Users.
Games get assigned to Users through the Assignments table. However, when a game needs to be assigned to a new user, I just make a new Assignment, and go off of the latest assignment for each game.
My end goal is to be able to grab a collection of games that any given User has assigned to them. However, to do that, I first need to be able to query Assignments and filter out any assignment that isn't the most recent for that given Game id.
Any thoughts? I've been trying some things (see below) but not getting anywhere.
Game function (works):
public function latestAssignment() {
return $this->hasOne('App\Models\Game_Assignment', 'game_id')->latest();
}
Game_Assignment function:
public function isLatestAssignment() {
if($this->game->latestAssignment()->id == $this->id) {
return true;
} else {
return false;
}
}
User function (throws error):
public function current_game_assignments() {
return $this->hasMany('App\Models\Game_Assignment', 'statistician_id')->where('isLatestAssignment', true);
}
Let me know what y'all think I can do!