/** * @param CategoryRepository $categoryRepository * @param string $issue * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function index(CategoryRepository $categoryRepository, $issue) { $issueArticles = $this->articleRepository->articles($issue); $recommendedCategoryId = $categoryRepository->recommendedCategoryIdWithCache(); $otherCategoryId = $categoryRepository->otherCategoryIdWithCache(); $recommArticles = []; $normalArticles = collect(); // 创建一个新集合 $otherArticles = []; foreach ($issueArticles as $key => $value) { if ($key == $recommendedCategoryId) { // 推荐分类文章 $recommArticles = $value; } elseif ($key == $otherCategoryId) { // 其他分类文章 $otherArticles = $value; } else { $normalArticles = $normalArticles->merge($value); } } // 把推荐分类文章放在集合第一个 其他分类文章放在集合最后一个 $articles = collect(); $articles = $articles->merge($recommArticles)->merge($normalArticles)->merge($otherArticles)->groupBy('category_id'); $latestIssues = $this->issueRepository->getIssues(); return view('frontend.issues.index', compact('issue', 'articles', 'latestIssues')); }