Ver Fonte

Lesson 20: archives

Frederic G. MARAND há 6 anos atrás
pai
commit
ada38c810a

+ 18 - 4
app/Http/Controllers/PostsController.php

@@ -3,6 +3,7 @@
 namespace App\Http\Controllers;
 
 use App\Post;
+use Carbon\Carbon;
 use Illuminate\Http\Request;
 
 class PostsController extends Controller
@@ -53,11 +54,24 @@ class PostsController extends Controller
      *
      * @return \Illuminate\Http\Response
      */
-    public function index()
+    public function index(Request $request)
     {
-        // $posts = Post::orderBy('created_at', 'desc')->get();
-        $posts = Post::latest()->get();
-        return view('posts.index', compact('posts'));
+        $posts = Post::latest()
+            ->filter($request->only(['month', 'year']))
+            ->get();
+
+        // Temporarily here: will be moved when discussing view composers.
+        $archives = Post::selectRaw(<<<EOT
+year(created_at) as year,
+monthname(created_at) as month,
+count(*) published
+EOT
+        )->groupBy('year', 'month')
+            ->orderByRaw('min(created_at) desc')
+            ->get()
+            ->toArray();
+
+        return view('posts.index', compact('archives', 'posts'));
     }
 
     public function show(Post $post)

+ 26 - 0
app/Post.php

@@ -2,6 +2,8 @@
 
 namespace App;
 
+use Carbon\Carbon;
+
 class Post extends Model
 {
 
@@ -34,4 +36,28 @@ class Post extends Model
     {
         return $this->belongsTo(User::class);
     }
+
+    /**
+     * Enables a "filter()" method on Post queries.
+     */
+    public function scopeFilter($query, array $filters = [])
+    {
+        if ($month = $filters['month'] ?? NULL)
+        {
+            /* When parsing just a month name, Carbon returns a date built
+            from the current date at midnight, with the month replaced by the
+            specified month name */
+            $query->whereMonth('created_at', Carbon::parse($month)->month);
+        }
+
+        if ($year = (int) $filters['year'] ?? NULL)
+        {
+            /* When parsing just a month name, Carbon returns a date built
+            from the current date at midnight, with the month replaced by the
+            specified month name */
+            $query->whereYear('created_at', $year);
+        }
+
+        return $query;
+    }
 }

+ 5 - 12
resources/views/layouts/sidebar.blade.php

@@ -6,18 +6,11 @@
     <div class="sidebar-module">
         <h4>Archives</h4>
         <ol class="list-unstyled">
-            <li><a href="#">March 2014</a></li>
-            <li><a href="#">February 2014</a></li>
-            <li><a href="#">January 2014</a></li>
-            <li><a href="#">December 2013</a></li>
-            <li><a href="#">November 2013</a></li>
-            <li><a href="#">October 2013</a></li>
-            <li><a href="#">September 2013</a></li>
-            <li><a href="#">August 2013</a></li>
-            <li><a href="#">July 2013</a></li>
-            <li><a href="#">June 2013</a></li>
-            <li><a href="#">May 2013</a></li>
-            <li><a href="#">April 2013</a></li>
+            @foreach ($archives ?? [] as $archive)
+            <li><a href="/?month={{ $archive['month'] }}&year={{ $archive['year'] }}">
+                    {{ $archive['month'] . ' ' . $archive['year'] }}
+                </a></li>
+            @endforeach
         </ol>
     </div>
     <div class="sidebar-module">