<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # 準備工作 1.設計三張表users,posts,comments,表結構如下:# users ~~~ Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); ~~~ posts ~~~ Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->integer('user_id')->index(); $table->text('content'); $table->timestamps(); }); ~~~ comments 其中parent_id主要表示該評論所屬評論的id,如果不屬于任何評論則為null ~~~ Schema::create('comments', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->index(); $table->integer('post_id')->index(); $table->integer('parent_id')->index()->nullable(); $table->text('body'); $table->timestamps(); }); ~~~ # 表之間的關系如下 Post.php文件 ~~~ /** * 一篇文章有多個評論 * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function comments() { return $this->hasMany(Comment::class); } /** * 獲取這篇文章的評論以parent_id來分組 * @return static */ public function getComments() { return $this->comments()->with('owner')->get()->groupBy('parent_id'); } ~~~ Comments.php文件 ~~~ /** * 這個評論的所屬用戶 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function owner() { return $this->belongsTo(User::class, 'user_id'); } /** * 這個評論的子評論 * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function replies() { return $this->hasMany(Comment::class, 'parent_id'); } ~~~ # 邏輯編寫 我們所要實現的嵌套評論其實在我們準備工作中已經 有點思路了,我們首先將一篇文章顯示出來,同時利用文章與評論的一對多關系,進行顯示所有的評論,但是我們的評論里面涉及到一個字段就是parent_id,這個字段其實非常的特殊,我們利用這個字段來進行分組, 代碼就是上面的`return $this->comments()->with('owner')->get()->groupBy('parent_id')`,具體的過程如下: web.php文件 `\Auth::loginUsingId(1); //用戶id為1的登錄` //顯示文章和相應的評論 ~~~ Route::get('/post/show/{post}', function (\App\Post $post) { $post->load('comments.owner'); $comments = $post->getComments(); $comments['root'] = $comments['']; unset($comments['']); return view('posts.show', compact('post', 'comments')); }); ~~~ //用戶進行評論 ~~~ Route::post('post/{post}/comments', function (\App\Post $post) { $post->comments()->create([ 'body' => request('body'), 'user_id' => \Auth::id(), 'parent_id' => request('parent_id', null), ]); return back(); }); ~~~ # 視圖代碼 視圖方面我們需要實現嵌套,那么隨著用戶互相評論的越來越多的話,那么嵌套的層級也就越多,所以說,我們這里需要使用各小技巧來顯示整個評論,我們使用@include()函數來顯示,那么我們試圖的結構如下: comments comments.blade.php form.blade.php list.blade.php posts show.blade.php 代碼如下: show.blade.phpa ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container" style="margin-top: 100px"> <div class="col-md-10 col-md-offset-1"> <h2>{{$post->title}}</h2> <h4>{{$post->content}}</h4> <hr> @include('comments.list',['collections'=>$comments['root']]) <h3>留下您的評論</h3> @include('comments.form',['parentId'=>$post->id]) </div> </div> </body> </html> ~~~ comment.blade.php ~~~ <div class="col-md-12"> <h5><span style="color:#31b0d5">{{$comment->owner->name}}</span>:</h5> <h5>{{$comment->body}}</h5> @include('comments.form',['parentId'=>$comment->id]) @if(isset($comments[$comment->id])) @include('comments.list',['collections'=>$comments[$comment->id]]) @endif <hr> </div> ~~~ form.blade.php ~~~ <form method="POST" action="{{url('post/'.$post->id.'/comments')}}" accept-charset="UTF-8"> {{csrf_field()}} @if(isset($parentId)) <input type="hidden" name="parent_id" value="{{$parentId}}"> @endif <div class="form-group"> <label for="body" class="control-label">Info:</label> <textarea id="body" name="body" class="form-control" required="required"></textarea> </div> <button type="submit" class="btn btn-success">回復</button> </form> ~~~ list.blade.php ~~~ @foreach($collections as $comment) @include('comments.comment',['comment'=>$comment]) @endforeach ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看