فهرست منبع

Lesson 16: adding comments.

Frederic G. MARAND 6 سال پیش
والد
کامیت
6309bed5a5

+ 97 - 0
app/Http/Controllers/CommentsController.php

@@ -0,0 +1,97 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Comment;
+use App\Post;
+use Illuminate\Http\Request;
+
+class CommentsController extends Controller
+{
+
+    /**
+     * Show the form for creating a new resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function create()
+    {
+        //
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  int $id
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy($id)
+    {
+        //
+    }
+
+    /**
+     * Show the form for editing the specified resource.
+     *
+     * @param  int $id
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function edit($id)
+    {
+        //
+    }
+
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index()
+    {
+        //
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  int $id
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function show($id)
+    {
+        //
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request $request
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Post $post, Request $request)
+    {
+        $this->validate($request, ['body' => 'required|min:2']);
+        $post->addComment($request->get('body'));
+
+        // return redirect("/posts/{$post->id}");
+        return back();
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request $request
+     * @param  int $id
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, $id)
+    {
+        //
+    }
+
+}

+ 14 - 0
app/Post.php

@@ -2,6 +2,8 @@
 
 namespace App;
 
+use Illuminate\Http\Request;
+
 class Post extends Model {
 
   /**
@@ -11,6 +13,18 @@ class Post extends Model {
    */
   protected $fillable = ['body', 'title'];
 
+    /**
+     * @param string $body
+     */
+    public function addComment(string $body) {
+//      Comment::create([
+//          'body' => $body,
+//          'post_id' => $this->id,
+//      ]);
+
+        $this->comments()->create(compact('body'));
+  }
+
   public function comments()
   {
       return $this->hasMany(Comment::class);

+ 1 - 1
composer.json

@@ -5,7 +5,7 @@
     "license": "MIT",
     "type": "project",
     "require": {
-        "php": ">=5.6.4",
+        "php": ">=7.0",
         "laravel/framework": "5.4.*",
         "laravel/tinker": "~1.0"
     },

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 5 - 0
public/css/bootstrap.min.css


+ 1 - 1
resources/views/layouts/master.blade.php

@@ -11,7 +11,7 @@
 
 
         <!-- Bootstrap core CSS -->
-        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
+        <link rel="stylesheet" href="/css/bootstrap.min.css" />
 
         <!-- Custom styles for this template -->
         <link rel="stylesheet" href="/css/app.css" />

+ 34 - 12
resources/views/posts/show.blade.php

@@ -10,18 +10,40 @@
 
         {{ $post->body }}
         @if (count($post->comments))
-        <hr />
-        <div class="comments">
-            <ul class="list-group">
-                @foreach($post->comments as $comment)
-                <li class="list-group-item">
-                    <strong>{{ $comment->created_at->diffForHumans() }}</strong>
-                    :&nbsp;
-                    {{ $comment->body }}
-                </li>
-                @endforeach
-            </ul>
-        </div>
+            <hr />
+            <div class="comments">
+                <ul class="list-group">
+                    @foreach($post->comments as $comment)
+                        <li class="list-group-item">
+                            <strong>{{ $comment->created_at->diffForHumans() }}</strong>
+                            :&nbsp;
+                            {{ $comment->body }}
+                        </li>
+                    @endforeach
+                </ul>
+            </div>
         @endif
+
+        {{-- Add a comment --}}
+        <div class="card">
+            <div class="card-block">
+                <form method="post" action="/posts/{{ $post->id }}/comments">
+                    {{-- Only needed for methods other than GET/POST, since most
+                         browsers only support those two --}}
+                    {{ method_field('POST') }}
+                    {{ csrf_field() }}
+
+                    @include('layouts.errors')
+                    <div class="form-group">
+                        <textarea required class="form-control" name="body" placeholder="Your comment here">
+                        </textarea>
+                    </div>
+
+                    <div class="form-group">
+                        <button type="submit" class="btn btn-primary">Add comment</button>
+                    </div>
+                </form>
+            </div>
+        </div>
     </div>
 @endsection

+ 2 - 0
routes/web.php

@@ -17,6 +17,8 @@ $this->post('/posts', 'PostsController@store');
 $this->get('/posts/create', 'PostsController@create');
 $this->get('/posts/{post}', 'PostsController@show');
 
+$this->post('/posts/{post}/comments', 'CommentsController@store');
+
 /* Reminder about typical REST routes
 
 GET /posts            List posts

برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است