[英]Laravel How to get multi different results by executing only one query
Laravel版本:7.0
$posts = Post::where("status", 1)->get();
以上返回所有已批准的帖子。
$top_featured_posts = Post::where("status", 1)->orderBy("featured", "DESC")->take(4);
以上将按特色获得 4 个帖子顺序。
$top_recent_posts = Post::where("status", 1)->latest()->take(4);
以上将通过 created_at 获得 4 个帖子订单。 ...
我怎样才能只用一个查询来获得它们?
我想得到如下结果来使用它们。
$top_featured_posts[0], $top_featured_posts[1], $top_featured_posts[2], $top_featured_posts[3]
$top_recent_posts[0], $top_recent_posts[1], $top_recent_posts[2], $top_recent_posts[3]
谁能帮我? 谢谢
你需要试试这个:
$posts = Post::where("status", 1)->get();
$top_featured_posts = $posts->sortByDesc("featured", "DESC")->take(4)->values();
$top_recent_posts = $posts->sortByDesc("created_at")->take(4)->values();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.