From c3bd519bae77548a642ff78e32eff9f058cca5cd Mon Sep 17 00:00:00 2001
From: Amelia Priti Usparida <ameliapriti@student.uns.ac.id>
Date: Wed, 13 Apr 2022 12:04:44 +0700
Subject: [PATCH] coba

---
 app/Models/Category.php              |  5 +++++
 resources/views/categories.blade.php | 17 +++++++++++++++++
 resources/views/category.blade.php   | 15 +++++++++++++++
 resources/views/post.blade.php       | 13 +++++++------
 routes/web.php                       | 16 +++++++++++++++-
 5 files changed, 59 insertions(+), 7 deletions(-)
 create mode 100644 resources/views/categories.blade.php
 create mode 100644 resources/views/category.blade.php

diff --git a/app/Models/Category.php b/app/Models/Category.php
index 2867e78..4f3db8a 100644
--- a/app/Models/Category.php
+++ b/app/Models/Category.php
@@ -10,4 +10,9 @@ class Category extends Model
     use HasFactory;
 
     protected $guarded = ['id'];
+
+    public function post()
+    {
+        return $this->hasMany(Post::class);
+    }
 }
diff --git a/resources/views/categories.blade.php b/resources/views/categories.blade.php
new file mode 100644
index 0000000..f54d4f3
--- /dev/null
+++ b/resources/views/categories.blade.php
@@ -0,0 +1,17 @@
+@extends('layouts/main')
+
+@section('container')
+<h1 class="mb-5">Post Categories : {{ $category}}</h1>
+
+    @foreach ($categories as $categories)
+    ,ul>
+        <li>
+        <h2><a href="/categories/{{ $category->slug }}">{{ $category->name}}</a></h2>
+        </li>
+    </ul>
+        
+
+    @endforeach
+
+@endsection
+
diff --git a/resources/views/category.blade.php b/resources/views/category.blade.php
new file mode 100644
index 0000000..59c6d1f
--- /dev/null
+++ b/resources/views/category.blade.php
@@ -0,0 +1,15 @@
+@extends('layouts/main')
+
+@section('container')
+<h1 class="mb-5">Post Category : {{ $category}}</h1>
+
+    @foreach ($category as $category)
+    <article>
+        <h2><a href="/posts/{{ $post->slug }}">{{ $post->title}}</a></h2>
+        <p>{{ $post->excerpt}}</p>
+    </article>
+
+    @endforeach
+
+@endsection
+
diff --git a/resources/views/post.blade.php b/resources/views/post.blade.php
index 49c1654..dad0b5c 100644
--- a/resources/views/post.blade.php
+++ b/resources/views/post.blade.php
@@ -1,11 +1,12 @@
 @extends('layouts.main')
 
 @section('container')
-    <article>
-        <h2>{{ $post->title }}</h2>
-        <h5>{{ $post->author }}</h5>
-        {!! $post->body !!}
-    </article>
+    <h1 class="mb-5"{{ $post->title }}</h1>
+
+    <p>By. Amelia Priti Usparida in <a href="/categories/(($post->category->slug}}">{{ $post->category->name}}</a></p>
+
+    {!! $post->body !!}
+
+    <a href="/posts">Back to Posts</a>
 
-    <a href="/blog">Back to Posts</a>
 @endsection
\ No newline at end of file
diff --git a/routes/web.php b/routes/web.php
index b1bdc0f..93151f9 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -3,7 +3,7 @@
 use App\Models\Post;
 use Illuminate\Support\Facades\Route;
 use App\Http\Controllers\PostController;
-
+use App\Models\Category;
 
 /*
 |--------------------------------------------------------------------------
@@ -37,3 +37,17 @@
 //halaman single post
 Route::get('posts/{post:slug}', [PostController::class,'show']);
 
+Route::get('/categories', function(){
+    return view('category', [
+        'title' => 'Post Categories',
+        'categories' => Category::all()
+    ]);
+})
+
+Route::get('/categories/{category:slug}', function(Category $category){
+    return view('category', [
+        'title' => $category->name,
+        'posts' => $category->posts,
+        'category' => $category->name,
+    ]);
+});
\ No newline at end of file
-- 
GitLab