在Laravel中,如何根据儿童领域的总和来查询父模型?
我有两个型号,game
和圆形
。游戏有很多回合,一轮属于游戏。该结构如下所示。
{
"game":{
"title": "Game One",
"description": "A game one",
"rounds": [
{
"title": "Round 1",
"points": 10
},
{
"title": "Round 2",
"points": 10
},
{
"title": "Round 3",
"points": 10
}
]
}
}
我想根据所有回合的总点查询游戏。因此,在上面的示例中,总点将为 30 。我正在使用Jenssegers/Laravel-Mongodb
软件包,并想知道如何实现这一目标?
I have two models, Game
and Round
. A game has many rounds, and a round belongs to a game. The structure is something like the following.
{
"game":{
"title": "Game One",
"description": "A game one",
"rounds": [
{
"title": "Round 1",
"points": 10
},
{
"title": "Round 2",
"points": 10
},
{
"title": "Round 3",
"points": 10
}
]
}
}
I want to query the game according to the total points from all the rounds. So for the above example, the total points would be 30. I am using the jenssegers/laravel-mongodb
package and would like to know how I can achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请尝试以下代码:
请先在 game 模式中添加以下代码,以使其与 round 模态的关系
,然后在控制器中添加以下功能
please try the below code:
Please first add the below code in the Game modal for the relationship with the Round modal
Then add below function in your controller