소스 검색

Lesson 13: rendering posts.

Frederic G. MARAND 7 년 전
부모
커밋
b06f10fbde

+ 2 - 1
app/Http/Controllers/PostsController.php

@@ -49,7 +49,8 @@ class PostsController extends Controller
      */
     public function index()
     {
-        $posts = Post::all();
+        // $posts = Post::orderBy('created_at', 'desc')->get();
+        $posts = Post::latest()->get();
         return view('posts.index', compact('posts'));
     }
 

+ 0 - 2
public/info.php

@@ -1,2 +0,0 @@
-<?php
-phpinfo();

+ 1 - 6
resources/views/posts/index.blade.php

@@ -3,12 +3,7 @@
 @section('content')
     <div class="col-sm-8 blog-main">
         @foreach ($posts as $post)
-            <div class="blog-post">
-                <h2 class="blog-post-title"><a href="/posts/{{ $post->id }}">{{ $post->title }}</a></h2>
-                <p class="blog-post-meta">{{ $post->created_at }} by <a href="#">Mark</a></p>
-
-                {{ $post->body }}
-            </div><!-- /.blog-post -->
+            @include('posts.post')
         @endforeach
 
         <nav class="blog-pagination">

+ 10 - 0
resources/views/posts/post.blade.php

@@ -0,0 +1,10 @@
+<div class="blog-post">
+    <h2 class="blog-post-title">
+        <a href="/posts/{{ $post->id }}">{{ $post->title }}</a>
+    </h2>
+    <p class="blog-post-meta">
+        {{ $post->created_at->toFormattedDateString() }}
+        by <a href="#">Mark</a>
+    </p>
+    {{ $post->body }}
+</div>

+ 1 - 5
resources/views/posts/show.blade.php

@@ -2,10 +2,6 @@
 
 @section('content')
     <div class="col-sm-8 blog-main">
-        <div class="blog-post">
-            <h2 class="blog-post-title">{{ $post->title }}</h2>
-            <p class="blog-post-meta">{{ $post->updated_at }} by <a href="#">Mark</a></p>
-            {{ $post->body }}
-        </div>
+        @include('posts.post')
     </div>
 @endsection