Maintain the value of the variable when opening a modal

0

I have a table in which to each row I pass some variables through a foreach.

And when I give 'Delete' a modal opens, that has the first value of the table, it does not matter if I click on the delete button of the row with ID 5, that I will get the value of ID 1 .

The code is this:

<tbody>
     @foreach ($admins as $key => $admin)
      <tr>
        <td class="idadmin tdcenter"><p id="margindata" class="tdmenuadmin">{{$admin->id}}</p></td>
        <td class="nameadmin"><p id="margindata" class="tdmenuadmin">{{$admin->name}}</p></td>
        <td class="emailadmin"><p id="margindata" class="tdmenuadmin">{{$admin->email}}</p></td>
        <td class="actionsadmin tdmenuadmin">

         <button type="button" class="btn btn-danger btn-sm deletemenuadmin" data-toggle="modal" data-target="#formdeleteadmin" id="margindata">Delete</button>

         <div id="formdeleteadmin" class="modal fade" role="dialog"> <!-- DIV TO SHOW THE CREATE PROJECT FORM 1 START HERE-->

         <div class="modal-dialog" style="background-color:#23517F;">

         <div class="modal-content" style="background-color:#23517F;">

            <div class="modal-header">

            <button type="button" class="close" data-dismiss="modal">&times;</button>

            <h4 class="modal-title" style="color:black;">{{$admin->id}}¿Estas seguro de borrar al administrador?</h4>

            </div>

            <div class="modal-body">

                <div class="col-sm-6">
                            <a href="{{ route('admin.admins') }}" class="btn btn-danger btn-block">No</a>
                </div>
                <div class="col-sm-6">
                    <form method="POST" action="{{route('admin.admins.destroy',$admin->id)}}">
                        <input type="submit" value="Si" class="btn btn-danger btn-block">
                        <input type="hidden" name="_token" value="{{Session::token()}}">
                        {{method_field('DELETE')}}
                    </form>
                </div>
            </div>

            <div class="modal-footer">

                    <button type="button" class="btn btn-default" data-dismiss="modal" id="closemodal">Close</button>

            </div>

         </div>

       </div>

     </div>
        </td>
      </tr>
    @endforeach
</tbody>

When I give the Delete it shows me this modal: (it's clipped)

And if I give to if it goes to the route that indicates the code and executes the function of the controller, the function is this:

 public function destroyAdmin($id) //Eliminar la informacion de un admin
    {
        $admin = Admin::find($id);
        $admin->delete();
        Session::flash('success','El admin ha sido eliminado con éxito.');
        return redirect()->route('admin.admins');
    } 

How can I open the modal when I open the ID of the row?

    
asked by Lluís Puig Ferrer 18.10.2017 в 16:40
source

1 answer

1

What is happening in your case is that you are always opening the same modal #formdeleteadmin , remember that an id is unique and unrepeatable on the site and when for some reason you repeat it the code will always interact with the first one find ignoring others.

Take advantage of your foreach indexes to generate unique ids like this:

<button type="button" class="btn btn-danger btn-sm deletemenuadmin" data-toggle="modal" data-target="#formdeleteadmin_{{ ($key + 1) }}" id="margindata">Delete</button>

<div id="formdeleteadmin_{{ ($key + 1) }}" class="modal fade" role="dialog">
    
answered by 18.10.2017 / 16:47
source