我在“评论,游戏和信息”表格之间创建了一个关系,不幸的是,主要表格是游戏,他命令我全部用于游戏,而我想订购“评论”表的ID。
Models: Review
public function games()
{
return $this->hasMany('App\Models\Giochi', 'id_gioco', 'id');
}
Models: Giochi
public function review()
{
return $this->belongsTo('App\Models\Review', 'id', 'id_gioco');
}
public function infogiochi()
{
return $this->belongsTo('App\Models\InfoGiochi', 'id', 'id_gioco');
}
Models: InfoGiochi
public function games()
{
return $this->hasMany('App\Models\Giochi', 'id', 'id_gioco');
}
Controller:
$review = Giochi::with('Review','InfoGiochi')->orderBy('id','DESC')->get();
以下是我的json的截图:
如何为评论表ID订购内容?
答案 0 :(得分:1)
您有2个选项。您可以在sql语句中使用连接和顺序,或者在检索集合中的结果后对其进行排序。
使用加入
Giochi::with('Review','InfoGiochi')
->get()
->sortByDesc(function($giochi) {
return $giochi->review->id;
});
排序集合
df<- structure(list(X..taminn = c(86L, 102L, 34L, 35L, 53L, 54L, 43L,
31L, 62L, 54L), X..turtia = c(413L, 322L, 525L, 535L, 247L, 207L,
241L, 440L, 189L, 221L), X..bestt = c(82L, 102L, 6L, 2L, 46L,
1L, 39L, 7L, 62L, 54L), Best..F1..129..test = c(3079.3, 1512.9,
497, 2804.5, 1353.7, 1597.6, 2005.1, 186.4, 1847.5, 1558.2),
best..F1..130..test = c(8972.7, 1944, 2022.7, 13001.7, 2085,
3228.6, 4948.5, 943.6, 1329, 2655.9), best..F1..131..test = c(2634.4,
1181.3, 505.2, 2802.4, 1573.2, 1707.6, 1585.9, 192.1, 809.2,
1219), how1 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
2L, 1L), .Label = c("", "High", "Not Found"), class = "factor"),
how2 = structure(c(2L, 2L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 1L
), .Label = c("", "High", "Not Found"), class = "factor"),
how3 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L
), .Label = c("", "High"), class = "factor"), Best.test...F1..129..test = c(3079.3,
1512.9, 497, 2804.5, 1353.7, 1597.6, 2005.1, 186.4, 1847.5,
1558.2), best.test...F1..130..test = c(8972.7, 1944, 2022.7,
13001.7, 2085, 3228.6, 4948.5, 943.6, 1329, 2655.9), best.test...F1..131..test = c(2634.4,
1181.3, 505.2, 2802.4, 1573.2, 1707.6, 1585.9, 192.1, 809.2,
1219), Best..test..129..test = c(3079.3, 1512.9, 497, 2804.5,
1353.7, 1597.6, 2005.1, 186.4, 1847.5, 1558.2), best..test..130..test = c(8972.7,
1944, 2022.7, 13001.7, 2085, 3228.6, 4948.5, 943.6, 1329,
2655.9), best..test..131..test = c(2634.4, 1181.3, 505.2,
2802.4, 1573.2, 1707.6, 1585.9, 192.1, 809.2, 1219)), .Names = c("X..taminn",
"X..turtia", "X..bestt", "Best..F1..129..test", "best..F1..130..test",
"best..F1..131..test", "how1", "how2", "how3", "Best.test...F1..129..test",
"best.test...F1..130..test", "best.test...F1..131..test", "Best..test..129..test",
"best..test..130..test", "best..test..131..test"), class = "data.frame", row.names = c(NA,
-10L))
答案 1 :(得分:0)
您可以尝试使用原始查询,也可以直接在审核功能中使用 - &gt; orderBy()。
答案 2 :(得分:0)
您可以在解雇时修改关系查询:
Giochi::with([
'Review' => function ($query) { return $query->orderBy('id','DESC'); },
'InfoGiochi'
])->orderBy('id','DESC');
答案 3 :(得分:0)
这是在集合上排序的最短版本:
Giochi::with('review')
->get()
->sortByDesc('review.id');