【发布时间】:2019-08-17 23:13:21
【问题描述】:
我有两个模型:Game 和 Game_Assignment。 Game_Assignment 告诉谁是玩游戏的人。
我正在尝试计算用户拥有其 ID 的 Game_Assignment 的数量,该数量在与之相关的游戏模型上也具有特定值。我将进入模型/代码
游戏模型关系:
public function assignments() {
return $this->hasMany('App\Models\Game_Assignment', 'game_id');
}
Game_Assignment 关系:
public function game() {
return $this->belongsTo('App\Models\Game', 'game_id');
}
哪里出了问题(在队列作业中,如果有影响的话)
$gamesDue = Game_Assignment::where('statistician_id', $statistician->id)->game->where('stats_done', '!=', 'yes')->count();
我也尝试了以下两件事,都没有成功:
$gamesDue = Game_Assignment::where('statistician_id', $statistician->id)->game()->where('stats_done', '!=', 'yes')->count();
还有……
$gamesDue = Game_Assignment::where('statistician_id', $defaultStatistician->id)->with(['games' => function($query) {
$query->where('stats_done', '!=', 'yes');
}])->count();
这些都不起作用,我展示的第一个抛出了错误:
Property [game] does not exist on the Eloquent builder instance.
有人知道我哪里出错了吗?我使用这个链接作为我的参考https://laravel.com/docs/5.8/eloquent-relationships#eager-loading
【问题讨论】: