我有一個模型,我在其中排序專案
public static function getArticleBlocks() {
return ArticleBlock::orderBy('order', 'ASC')->get();
}
這個模型有一個父級Article,我在其中輸出資料和ArticleBlock資料
public static function getArticle($id) {
return Article::where('id', $id)->with(['blog_categories', 'article_recommendations', 'article_blocks', 'article_comments'])->first();
}
如何對article_blockswhen get進行相同的排序Article?
uj5u.com熱心網友回復:
如果你想對hasmany Relation記錄進行排序。您可以在 with 方法中將回呼作為陣列值傳遞。
sort_column根據您的要求重命名
public static function getArticle($id)
{
return Article::query()
->where('id', $id)
->with([
'blog_categories',
'article_recommendations',
'article_blocks' => function($query){
return $query->orderBy('sort_column','ASC');
},
'article_comments'
])
->first();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/367070.html
標籤:拉拉维尔
