首页 > 解决方案 > 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:

my view table

标签: phplaravel

解决方案


如评论中所述,idHTML 中的 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

要处理这个问题,只需使ids 独一无二:

...
<a ... data-target="#exampleModalCenter_{{ $cartera->id }}">
...
<div ... id="exampleModalCenter_{{ $cartera->id }}">

推荐阅读