所以这是我的代码,它是Laravel .blade.php,但无论如何,我的问题在于此HTML / PHP代码。
问题是,当我按下任何删除按钮时,我会得到相同的ID,我会得到cat-> id =1。当我在笔记本电脑行中按下“删除”按钮时,我应该在删除确认中输入id号:2,但我仍然得到数字:1,并与其他任何行一样
<div class="">
<div class="box">
<div class="box-header">
<h3 class="box-title">All Categories</h3>
</div>
<div class="box-body">
<table class="table table-responsive">
<thead>
<tr>
<th>Name</th>
<th>Id</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
@foreach($categories as $cat)
<tr>
<td>{{$cat->nombre}}</td>
<td>{{$cat->id}}</td>
<td>
<button class="btn btn-danger" data-catid={{$cat->id}} data-toggle="modal" data-target="#delete">Delete</button>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
对于使用以下代码的模态im:
<div class="modal modal-danger fade" id="delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title text-center" id="myModalLabel"><span>Delete Confirmation</span><</h4>
</div>
<form action="{{route('categories.destroy','test')}}" method="post">
{{method_field('delete')}}
{{csrf_field()}}
<div class="modal-body">
<p class="text-center">
<span>Are you sure you want to delete this {{ $cat->id}}?</span>
</p>
<input type="hidden" name="category_id" id="cat_id" value="">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" data-dismiss="modal"><span>No, Cancel</span></button>
<button type="submit" class="btn btn-warning"><span>Yes, Delete</span></button>
</div>
</form>
</div>
</div>
我按了“笔记本电脑”行上的删除按钮,当我应该获得ID号:2时得到ID号:1。这适用于每一行
答案 0 :(得分:0)
这是因为每次您单击删除按钮时,它将调用具有相同ID的相同模型。更改您的按钮ID
<button class="btn btn-danger" data-catid={{$cat->id}} data-toggle="modal" data-target="#delete-{{$cat->id}}">Delete</button>
还有模式ID
<div class="modal modal-danger fade" id="delete-{{$cat->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
注意::您可能需要将模式框保持在foreach循环中,以便将删除按钮映射到正确的模式框