回答

收藏

在Laravel中为博客生成存档列表

技术问答 技术问答 354 人阅读 | 0 人回复 | 2023-09-14

我正在尝试为博客文章生成一个存档列表。存档列表应按相反的时间顺序显示年份和日期,如下所示:
' ^1 W1 d  A! `$ d0 _" m2013 (21)
6 M) M" @: K' W  m" x    - May (2)
1 r# Q3 f" \, @" K( e" c: e' W: m    - April (3)6 s' A! p0 R* n' {/ W
    - March (5)+ E" a* Z" T; n4 l6 ~/ b
    - February (1), s9 A2 J9 l# ^7 Q: d6 a
    - January (10)+ V! y1 q, W2 |/ p3 r4 `
2012 (10)
! e+ k0 {1 d) L    - December (6)
* f# S3 o$ t1 b3 D4 ?6 T  c    - November (4)
3 {& c! x! i8 e2 d3 R# Z' g4 d" r里面()的数字是该时间段内的帖子数。选择年份或月份后,仅应显示该选定时间段内的博客文章。1 G# j) ^& U' X) P
到目前为止,我只能通过执行以下操作来找出每篇博客文章的年份和月份:
3 U+ L* C' k; |$posts = Post::all();
# T: \! h. \# i9 H6 t$archive = array();
8 W' s4 V& h, F  @. lforeach ($posts as $post) {% A* G! g- v" K. X/ u/ ]5 t! h
    $year = date('Y', strtotime($post->created_at));2 [* i7 p9 \' y) v
    $month = date('m', strtotime($post->created_at));4 W, f* M: Y3 ]( z, m5 s4 ^
}
% [  U, A$ t" r& s1 s4 a我如何实现上述目标?
+ \, H: r2 K7 E0 r; Z- i$ L9 z                ' F3 L, w& O" p
解决方案:6 a7 y7 f- I$ [7 {' I
               
# l4 C3 F, Q/ Q2 s( r$ h/ j* S4 u; c! a) F2 ~6 ~

7 O) y: T9 ?0 u* x7 E                为了在某??种导航面板中生成链接,您可以在数据库端进行大部分处理,而不用这样的查询来获取所有博客文章记录+ U  d7 n5 x0 i0 ?  S1 X* y  F
SELECT YEAR(created_at) year,
4 P4 Z5 |2 L1 T+ Y) E. @# u! M" B       MONTH(created_at) month,
4 v% K) ]! ~8 }( I       MONTHNAME(created_at) month_name,  c# Z$ ?, Q7 U8 d3 e8 p) S( ?0 s
       COUNT(*) post_count
$ S/ |& h  S  J0 v3 {2 q  FROM post
* R; S7 m. ]4 G) Y GROUP BY year, MONTH(created_at)
3 H4 a. a3 t$ H( e5 R ORDER BY year DESC, month DESC;
* S) C, G4 _3 j: K+ b- a5 L输出:
# ]6 y% X  }: K( H. `, v* l! u| YEAR | MONTH | MONTH_NAME | POST_COUNT |- ~( ]: E% M* t  k
------------------------------------------
+ V$ d/ t; r: a& U: W| 2013 |     5 |        May |          5 |
- F4 b. K; ?; t8 S| 2013 |     4 |      April |          3 |/ L! Y7 j3 b+ x3 G  Y3 @
| 2013 |     3 |      March |          4 |
* T# M2 u0 c' O$ b! \1 p, I/ _/ Y0 [| 2013 |     2 |   February |          3 |% ?, @* g$ d) B9 o
| 2013 |     1 |    January |          2 |, {9 |- f5 H' C; o) \# Z
| 2012 |    12 |   December |          2 |
  Z3 L* L! o5 P" M| 2012 |    11 |   November |          3 |
' N5 j  ~8 t6 Y0 c! {/ O我不是laravel的专家,但是应该使用类似的方法来实现7 q' q! o) L( O+ @& d2 K" T# u
$links = DB::table('post')
) z9 _; L7 w" q; T    ->select(DB::raw('YEAR(created_at) year, MONTH(created_at) month, MONTHNAME(created_at) month_name, COUNT(*) post_count'))
" R* P1 K: U" ^# e; M, P    ->groupBy('year')
- _4 G3 V5 ~" s  c3 I( G    ->groupBy('month')& e: z) M9 G# h5 m9 I. Y, S/ x. c  }
    ->orderBy('year', 'desc')
, j6 w/ p8 M9 _4 A# E% p; v    ->orderBy('month', 'desc')9 Q7 Z5 U1 J+ r' S
    ->get();
8 n' l' {/ @. ]) B" a2 F* U9 ]如果需要,您可以像这样将小计添加到年行中, e+ e- Q! e7 S. M6 L: i1 M
SELECT YEAR(created_at) year,# i. {- V, I! J, f; F
       MONTH(created_at) month,
+ {! H$ C  C, m: f1 d       MONTHNAME(created_at) month_name,6 e) q% b1 t0 c) ]* Q, o
       COUNT(*) post_count
0 Z/ q- }1 g+ b  FROM post" v8 c* p7 g& h, {2 n
GROUP BY year, MONTH(created_at)( i+ e, Y, C+ j: T; Y. V' w7 B
UNION ALL
1 [( E8 e7 V0 D' ISELECT YEAR(created_at) year,
+ N$ r1 k  Y2 T- f% I; i       13 month,+ S0 P( e0 p9 o% D3 e& O
       NULL month_name,
4 S! k, P% h% [. b$ V; o& t* j) B       COUNT(*) post_count
; Y. `- {5 d- W, F( `7 a  FROM post2 y4 t5 l. e/ @6 t- l" j
GROUP BY year. F4 h3 ~( d7 Q- c. [; U
ORDER BY year DESC, month DESC;+ F! s6 z! n9 L# n; \7 c$ S) r% p
输出:$ ?3 e9 N8 P1 b+ X
| YEAR | MONTH | MONTH_NAME | POST_COUNT |3 z' t3 w# R" n
------------------------------------------
; R2 l! x$ E" J5 y| 2013 |    13 |     (null) |         17 |. @* b- Y( H$ L" V. R* o0 U
| 2013 |     5 |        May |          5 |0 h$ ^  Q; v7 q- j& x% V( ?, |
| 2013 |     4 |      April |          3 |
! T) `8 j" @2 b- c: `| 2013 |     3 |      March |          4 |
9 v8 y" d  `  u  m( ]. d3 s* J| 2013 |     2 |   February |          3 |% ~- g9 S) R, t! U
| 2013 |     1 |    January |          2 |: N+ ]. |$ `7 k" N
| 2012 |    13 |     (null) |          5 |
4 Z+ T. P: E8 l* q: p$ e4 u| 2012 |    12 |   December |          2 |
# j- C' U8 S% v" r2 a  l6 V| 2012 |    11 |   November |          3 |
( z% T) L. O. @+ [- k& KSQLFiddle
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则