2
0

BlogController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // app/Http/Controllers/BlogController.php
  3. namespace App\Http\Controllers;
  4. use App\Models\Post;
  5. use App\Models\Category;
  6. use App\Models\Tag;
  7. use App\Models\ProgressChapter;
  8. use Illuminate\Http\Request;
  9. use Carbon\Carbon;
  10. use App\Http\Api\UserApi;
  11. use Illuminate\Support\Facades\Log;
  12. class BlogController extends Controller
  13. {
  14. // 首页 - 最新博文列表
  15. public function index($user)
  16. {
  17. $user = UserApi::getByName($user);
  18. $posts = ProgressChapter::with('channel')
  19. ->where('progress', '>', 0.9)
  20. ->whereHas('channel', function ($query) use ($user) {
  21. $query->where('status', 30)->where('owner_uid', $user['id']);
  22. })
  23. ->latest()
  24. ->paginate(10);
  25. Log::info($posts[0]->formatted_created_at);
  26. /*
  27. $posts = Post::published()
  28. ->with(['category', 'tags'])
  29. ->latest()
  30. ->paginate(10);
  31. $categories = Category::withCount('posts')->get();
  32. $popularPosts = Post::published()
  33. ->orderBy('views_count', 'desc')
  34. ->take(5)
  35. ->get();
  36. */
  37. //return view('blog.index', compact('posts', 'categories', 'popularPosts'));
  38. return view('blog.index', compact('user', 'posts'));
  39. }
  40. /*
  41. // 博文详情页
  42. public function show(Post $post)
  43. {
  44. if (!$post->is_published) {
  45. abort(404);
  46. }
  47. $post->incrementViews();
  48. $post->load(['category', 'tags']);
  49. // 相关文章
  50. $relatedPosts = Post::published()
  51. ->where('category_id', $post->category_id)
  52. ->where('id', '!=', $post->id)
  53. ->take(3)
  54. ->get();
  55. // 上一篇和下一篇
  56. $prevPost = Post::published()
  57. ->where('published_at', '<', $post->published_at)
  58. ->latest()
  59. ->first();
  60. $nextPost = Post::published()
  61. ->where('published_at', '>', $post->published_at)
  62. ->oldest()
  63. ->first();
  64. return view('blog.show', compact('post', 'relatedPosts', 'prevPost', 'nextPost'));
  65. }
  66. // 分类列表
  67. public function categories()
  68. {
  69. $categories = Category::withCount('posts')
  70. ->having('posts_count', '>', 0)
  71. ->orderBy('posts_count', 'desc')
  72. ->get();
  73. return view('blog.categories', compact('categories'));
  74. }
  75. // 分类下的文章
  76. public function category(Category $category)
  77. {
  78. $posts = Post::published()
  79. ->where('category_id', $category->id)
  80. ->with(['category', 'tags'])
  81. ->latest()
  82. ->paginate(10);
  83. return view('blog.category', compact('category', 'posts'));
  84. }
  85. // 年度归档
  86. public function archives()
  87. {
  88. $archives = Post::published()
  89. ->selectRaw('YEAR(published_at) as year, COUNT(*) as count')
  90. ->groupBy('year')
  91. ->orderBy('year', 'desc')
  92. ->get();
  93. return view('blog.archives', compact('archives'));
  94. }
  95. // 指定年份的文章
  96. public function archivesByYear($year)
  97. {
  98. $posts = Post::published()
  99. ->whereYear('published_at', $year)
  100. ->with(['category', 'tags'])
  101. ->latest()
  102. ->paginate(15);
  103. // 按月分组
  104. $postsByMonth = $posts->getCollection()->groupBy(function ($post) {
  105. return $post->published_at->format('Y-m');
  106. });
  107. return view('blog.archives-year', compact('posts', 'postsByMonth', 'year'));
  108. }
  109. // 标签页面
  110. public function tag(Tag $tag)
  111. {
  112. $posts = $tag->posts()
  113. ->published()
  114. ->with(['category', 'tags'])
  115. ->latest()
  116. ->paginate(10);
  117. return view('blog.tag', compact('tag', 'posts'));
  118. }
  119. // 搜索
  120. public function search(Request $request)
  121. {
  122. $query = $request->get('q');
  123. if (empty($query)) {
  124. return redirect()->route('blog.index');
  125. }
  126. $posts = Post::published()
  127. ->where(function ($q) use ($query) {
  128. $q->where('title', 'LIKE', "%{$query}%")
  129. ->orWhere('content', 'LIKE', "%{$query}%")
  130. ->orWhere('excerpt', 'LIKE', "%{$query}%");
  131. })
  132. ->with(['category', 'tags'])
  133. ->latest()
  134. ->paginate(10);
  135. return view('blog.search', compact('posts', 'query'));
  136. }
  137. */
  138. }