0

So this is my code, its a Laravel .blade.php but anyway my problem is with this HTML / PHP code.

The problem is that when I press on any delete button I get the same id, I get cat->id = 1. when I press the 'delete' button on the laptop row, I should get in my delete confirmation the id number: 2, but I still get the number: 1 and its that way with any other row

<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>

For the modal im using the next code:

<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">&times;</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>

I pressed the delete button on the Laptop row, and I get the id number:1 when I should get id number:2. this apply on every row

Sumithran
  • 6,217
  • 4
  • 40
  • 54
Xavier Basir
  • 23
  • 1
  • 5
  • You are creating only 1 modal based on a specific category ($cat->id), and you will always see a specific number. You have to adjust the category ID with JavaScript - on click of the delete button it should update the content of the modal. Also, pay attention you adjust the ACTION attribute of the form with JavaScript. – MyLibary Jul 31 '18 at 16:26
  • check this [answer](https://stackoverflow.com/questions/43524828/how-to-get-data-from-a-line-and-move-to-a-modal-laravel-5-4) – Bhaumik Pandhi Jul 31 '18 at 16:52

1 Answers1

0

This is because everytime you click on the delete button it calls the same model with the same ID. Change you Button ID

 <button class="btn btn-danger" data-catid={{$cat->id}} data-toggle="modal" data-target="#delete-{{$cat->id}}">Delete</button>

And the modal ID as well

<div class="modal modal-danger fade" id="delete-{{$cat->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">

NOTE: You might need to keep the modal box in foreach loop so that delete button is mapped to the correct modal box

Faizan Fayaz
  • 540
  • 5
  • 16