0

im new to laravel. So im trying to delete a record from database from my index page. But it only delete the record with the lowest primary key value, my id field. Here's my controller code for destroy function:

public function destroy(Province $province)
    {
        //

        $findProvince = DB::table('provinces')->where('id', $province->id)->delete();

        if($findProvince){
            return redirect()->route('provinces.index')->with('success', 'Province deleted successfully');
        }
        return back()->with('error', 'Province could not be deleted');

        //var_dump($province->id);
    }

Here's my view code:

@extends('layouts.app')

@section('content')
<div class="col-md-8 offset-md-2">
    <br/>
    <div>
        <a href="provinces/create" class="btn btn-primary">Add New Province</a>
    </div>
    <br/>
    <table class="table">
        <thead>
            <tr>
            <th scope="col">#</th>
            <th scope="col">Name</th>
            <th scope="col">Image</th>
            <th scope="col">Operations</th>
            </tr>
        </thead>
        <tbody>
            @foreach($provinces as $province)
                <tr>
                    <th scope="row">{{ $province->id }}</th>
                    <td>{{ $province->name }}</td>
                    <td><img src="../{{ $province->imgPath }}" width="200px"/></td>
                    <td>
                        <a href="provinces/{{ $province->id }}/edit" class="btn btn-warning">Edit</a> | 
                        <a
                                class="btn btn-danger"
                                href="#"
                                onclick="
                                  var result = confirm('Are you sure you wish to delete this province?');
                                      if( result ){
                                              event.preventDefault();
                                              document.getElementById('delete-form').submit();
                                      }
                                ">Delete
                        </a>

                        <form id="delete-form" action="{{route('provinces.destroy', $province->id)}}" method="POST" style="display: none;">
                            <input type="hidden" name="_method" value="delete">
                            {{ csrf_field() }}
                        </form>
                    </td>
                </tr>
            @endforeach

        </tbody>
    </table>
</div>
@endsection

I tried using var_dump on the $province->id but it only output the lowest 'id' value.

m5kev4n
  • 541
  • 1
  • 8
  • 20
  • If `$province->id` is the lowst value than the code works as expected? You have to alter `$province->id` to be the value of whatever you want it to be but you haven't posted the code were you declared `$province->id` – Tewdyn Jun 09 '18 at 08:18
  • Can you include the route definition as well? perhaps from `/routes/web.php` ? Also, actual snippet of HTML rendered by browser would help – Marius Jun 09 '18 at 08:33

2 Answers2

1

id's are unique. You will get this unexpected behavior if you use the same id in a loop. Either use unique id's or put your buttons inside the form (I suggest this).

<td>
    <form class="delete-form" action="{{ route('provinces.destroy', $province->id) }}" method="POST">
        <input type="hidden" name="_method" value="delete">
        {{ csrf_field() }}

        <a href="provinces/{{ $province->id }}/edit" class="btn btn-warning">Edit</a> | 
        <a class="btn btn-danger">Delete</a>
    </form>
</td>

And then use this Javascript in a seperate file for confirmation:

(function(){
    let forms = document.querySelectorAll('form.delete-form');
    [].forEach.call(forms, function(form){
        form.onsubmit = function(e) {
            if ( ! confirm('Are you sure you wish to delete this province?'))
                return e.preventDefault();
        }
    });
})();

If you still want to use your approach, add the id of $province to the id attribute of your form. This will make your form unique so you get access it with an id.

<form id="delete-form-{{ $province->id }}" ...>

And then your Javascript:

if( result ){
    event.preventDefault();
    document.getElementById('delete-form-{{ $province->id }}').submit();
}
Marwelln
  • 28,492
  • 21
  • 93
  • 117
0

Thanks everyone for your help. And sorry that it took me long to reply. I've been really busy with other schools. So i managed to track down the problem with my code. It's with my view. All the created delete form has the same id. So if i just added the record id to the form. It works just fine.

m5kev4n
  • 541
  • 1
  • 8
  • 20