hunterhawley
1 month ago

Dynamic Property call giving error (Property [game] does not exist on the Eloquent builder instance.)

Posted 1 month ago by hunterhawley

Hey there!

I have two models: Game and Game_Assignment. Game_Assignment tells whose job it is to play a game.

I am trying to count the number of Game_Assignment's that a user has their id on that also have a specific value on the Game model that it relates to. I'll just get into the Models/the code

Game Model Relationship:

public function assignments() {
  return $this->hasMany('App\Models\Game_Assignment', 'game_id');
}

Game_Assignment Relationship:

public function game() {
  return $this->belongsTo('App\Models\Game', 'game_id');
}

Where things are going wrong (in a queue job, if that makes a difference)

$gamesDue = Game_Assignment::where('statistician_id', $statistician->id)->game->where('stats_done', '!=', 'yes')->count();

I have also tried the following two things, neither worked:

$gamesDue = Game_Assignment::where('statistician_id', $statistician->id)->game()->where('stats_done', '!=', 'yes')->count();

and...

$gamesDue = Game_Assignment::where('statistician_id', $defaultStatistician->id)->with(['games' => function($query) {
        $query->where('stats_done', '!=', 'yes');
      }])->count();

None of these work, and the first one I showed threw an error:

Property [game] does not exist on the Eloquent builder instance.

Anyone have an idea of where I am going wrong? I am using this link as my reference https://laravel.com/docs/5.8/eloquent-relationships#eager-loading

Please sign in or create an account to participate in this conversation.