编辑评论仅适用于第一条评论



我有一个不同的页面用于编辑评论 - comments/{$id}/edit,我正在尝试将其添加到与我的评论相同的页面中的模态中。问题是,当我打开我的模态时,即使我所有评论都有按钮,我也只收到 5 条评论的第一条评论。这是怎么回事?

我的视图+模态

 <div class="col-md-12">

          @foreach($post->comments()->latest()->paginate(5) as $comment)
            <div class="media g-mb-30">
  <img class="d-flex g-width-50 g-height-50 rounded-circle g-mt-3 g-mr-20" src="/storage/{{ $comment->image }}" alt="Image Description">
  <div class="media-body g-brd-around g-brd-gray-light-v4 g-pa-30" style="margin-right: -35px">
    <div class="g-mb-15">
      <h5 class="d-flex justify-content-between align-items-center h5 g-color-gray-dark-v1 mb-0">
        <span class="d-block g-mr-10">{{ $comment->username }}
         <span class="g-color-black-opacity-0_7 g-pos-rel g-top-2 mx-2">&#183;</span>
<span class="g-color-gray-dark-v4 g-font-size-12">{{ $comment->created_at }}</span>
       </span>
        <a class="u-tags-v1 g-font-size-12 g-brd-around g-brd-gray-light-v4 g-bg-primary--hover g-brd-primary--hover g-color-black-opacity-0_8 g-color-white--hover rounded g-py-6 g-px-15" href="/profile/{{ $comment->user_id }}">Author</a>
      </h5>
    </div>

    <p>{{$comment->comment}}</p>
    <ul class="list-inline d-sm-flex my-0">
        @can('update', $post->user->profile)
      <li class="list-inline-item ml-auto">
        <a href="#" class="u-link-v5 g-color-gray-dark-v4 g-color-primary--hover"  data-toggle="modal" data-target="#exampleModal2">
          <i class="icon-note g-pos-rel g-top-1 g-mr-3"></i>
          Edit comment
        </a>
         <!-- Modal -->
<div class="modal fade" id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">

<div class="col-md-8 colm-d-offset-2">
<h1>edit comment</h1>
{{ Form::model($comment, ['route' => ['comments.update', $comment->id] , 'method' => 'PUT']) }}
 {{ Form::label('comment', "Comment:") }}
 {{ Form::textarea('comment', null, ['class' => 'form-control']) }}
  {{ Form::submit('Update Comment', ['class' => 'btn btn-success btn-xs']) }}
{{ Form::close() }}
</div>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
      </li>
<li class="list-inline-item ml-auto">
        <a class="u-link-v5 g-color-gray-dark-v4 g-color-primary--hover" href="/deleteComment/{{$comment->id}}">
          <i class="icon-note g-pos-rel g-top-1 g-mr-3"></i>
          Delete comment
        </a>
      </li>
      @endcan
    </ul>
  </div>
</div>

          @endforeach
            {{ $post->comments()->latest()->paginate(5)->links() }}

        </div>
      </div>
</div>
    </div>
  </div>
</div>
**

这是我的旧路线:**

Route::get('/comments/{id}/edit', ['uses' => 'CommentsController@edit', 'as' => 'comments.edit']);

我的控制器

 public function edit($id)
    {
        $comment = Comment::find($id);
        return view ('comments.edit')->withComment($comment);
    }
public function update(Request $request, $id)
    {
        $comment = Comment::find($id);
        $this->validate($request, array('comment' => 'required'));
        $comment->comment = $request->comment;
        $comment->save();
        Session::flash('success', 'Comment updated');

        return redirect('/post/' . $comment->post->id);
    }

您必须设置id="{!! $comment->id !!}

   <a id="{!! $comment->id !!}" href="#" class="u-link-v5 g-color-gray-dark-v4 g-color-primary--hover"  data-toggle="modal" data-target="#exampleModal2">
      <i class="icon-note g-pos-rel g-top-1 g-mr-3"></i>
      Edit comment
    </a>

如果你想通过$(this).attr("id");访问id并将其发送到modal或ajaxController

最新更新