我有另一页用于编辑注释-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">·</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">×</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);
}
答案 0 :(得分:0)
您必须设置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