Browse Source

Lesson 31: Sorting posts by tags.

Frederic G. MARAND 6 years ago
parent
commit
0fda2805f8

+ 16 - 0
app/Http/Controllers/TagsController.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Post;
+use App\Tag;
+use Illuminate\Http\Request;
+
+class TagsController extends Controller
+{
+    public function index(Tag $tag)
+    {
+        $posts = $tag->posts;
+        return view('posts.index', compact('posts'));
+    }
+}

+ 5 - 1
app/Providers/AppServiceProvider.php

@@ -4,6 +4,7 @@ namespace App\Providers;
 
 use App\Billing\Stripe;
 use App\Post;
+use App\Tag;
 use Illuminate\Foundation\Application;
 use Illuminate\Support\ServiceProvider;
 
@@ -22,7 +23,10 @@ class AppServiceProvider extends ServiceProvider
     public function boot()
     {
         view()->composer('layouts.sidebar', function ($view) {
-            $view->with('archives', Post::archives());
+            $archives = Post::archives();
+            $tags = Tag::has('posts')->pluck(Tag::LABEL_KEY);
+
+            $view->with(compact(['archives', 'tags']));
         });
     }
 

+ 8 - 1
app/Tag.php

@@ -6,7 +6,14 @@ use Illuminate\Database\Eloquent\Model;
 
 class Tag extends Model
 {
-    protected $fillable = ['name'];
+    const LABEL_KEY = 'name';
+
+    protected $fillable = [self::LABEL_KEY];
+
+    public function getRouteKeyName()
+    {
+        return static::LABEL_KEY;
+    }
 
     public function posts()
     {

+ 9 - 0
resources/views/layouts/sidebar.blade.php

@@ -3,6 +3,15 @@
         <h4>About</h4>
         <p>Etiam porta <em>sem malesuada magna</em> mollis euismod. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur.</p>
     </div>
+    <div class="sidebar-module">
+        <h4>Tags</h4>
+        <ol class="list-unstyled">
+            @foreach($tags ?? [] as $tag)
+                <li><a href="/posts/tags/{{ $tag }}">{{ $tag }}</a></li>
+            @endforeach
+        </ol>
+    </div>
+
     <div class="sidebar-module">
         <h4>Archives</h4>
         <ol class="list-unstyled">

+ 8 - 0
resources/views/posts/show.blade.php

@@ -8,6 +8,14 @@
             by <a href="#">{{ $post->user->name }}</a>
         </p>
 
+        @if (count($post->tags))
+            <ul>
+            @foreach ($post->tags as $tag)
+                <li><a href="/posts/tags/{{ $tag->name }}">{{ $tag->name }}</a></li>
+            @endforeach
+            </ul>
+        @endif
+
         {{ $post->body }}
         @if (count($post->comments))
             <hr />

+ 2 - 0
routes/web.php

@@ -27,10 +27,12 @@ $this->post('/posts', 'PostsController@store');
 $this->get('/posts/create', 'PostsController@create');
 $this->get('/posts/{post}', 'PostsController@show');
 $this->post('/posts/{post}/comments', 'CommentsController@store');
+$this->get('/posts/tags/{tag}', 'TagsController@index');
 
 $this->get('register', 'RegistrationController@create');
 $this->post('register', 'RegistrationController@store');
 
+
 $this->get('login', 'SessionsController@create');
 $this->post('login', 'SessionsController@store');
 $this->get('logout', 'SessionsController@destroy');