php - Registration is not deleted using a confirmation modality in laravel
问题描述
I have a table of records that when I click on the delete button in one of those records, a bootstrap 4 modal opens to confirm the action. The problem is that it always deletes the last record created, if I want to delete the first or second one, it also deletes the last one ... I've only seen what happens when I have the modal added and if it works fine without the modal. I was seeing that it always takes the id of the last record but I don't know why it happens ...
This es the code view with the modal:
<table class="table text-center">
<thead>
<th>Id</th>
<th>Nombre</th>
<th>Departamento</th>
<th>Municipio</th>
<th>Opciones</th>
</thead>
<tbody>
@foreach ($carteras as $cartera)
<tr>
<td>{{$cartera->id}}</td>
<td>{{$cartera->name}}</td>
<td>{{$cartera->depto}}</td>
<td>{{$cartera->municipio}}</td>
<td>
<a href="" class="badge-md badge-pill badge-success">Ver</a>
<a href="{{route('carteras.edit', $cartera->id)}}" class="badge-md badge-pill badge-primary">Editar</a>
<a href="" class="badge-md badge-pill badge-danger" data-toggle="modal" data-target="#exampleModalCenter">Eliminar</a>
</td>
</tr>
<!------ THIS IS THE MODAL ------>
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header d-flex justify-content-center">
</div>
<div class="modal-body">
<p class="text-center">Está seguro(a) de eliminar la cartera {{$cartera->name}} / {{$cartera->id}}?</p>
</div>
<div class="modal-footer d-flex justify-content-center">
<button type="button" class="btn btn-secondary btn-default" data-dismiss="modal">Cancelar</button>
{!! Form::open(['method'=>'DELETE', 'action'=>['CarterasController@destroy', $cartera->id]]) !!}
{!! Form:: submit('Eliminar', ['class'=>'btn btn-danger btn-default'])!!}
{!! Form::close() !!}
</div>
</div>
</div>
</div><!--fin modal-->
@endforeach
</tbody>
</table>
The controller:
public function destroy($id) {
$cartera = Cartera::findOrFail($id)->delete();
return redirect()->route('carteras.index');
}
And this the view:
解决方案
如评论中所述,id
HTML 中的 s 必须是唯一的,因此在循环中使用相同的值会导致无效的 HTML 和针对该 id 的意外结果:
@foreach ($carteras as $cartera)
...
<a href="" class="badge-md badge-pill badge-danger" data-toggle="modal" data-target="#exampleModalCenter">Eliminar</a>
...
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
@endforeach
要处理这个问题,只需使id
s 独一无二:
...
<a ... data-target="#exampleModalCenter_{{ $cartera->id }}">
...
<div ... id="exampleModalCenter_{{ $cartera->id }}">
推荐阅读
- java - Spring Boot 应用程序中所有请求的超时异常
- performance - 提高ESC/POS Thermal_Printer图像打印速度的方法?
- c - 假设给定数组 p[1......N] 和 q[1......N] 都未初始化,即每个位置可能包含任意值),
- asp.net-core - “没有为此 DbContext 配置数据库提供程序”
- javascript - 如何过滤对象并更新过滤的内容?JavaScript
- r - 如何添加将另一列解释为时间段的列?
- c - C/Linux:在线程之间交替
- python - 如何打印列表中的数据?
- memory - Redis 内存限制为只读
- javascript - POST 方法未插入用户数组