1

I found a different error. I tried to delete book by id. i have data for example

--------------
| id |  name |
--------------
| 1  | book1 |
| 2  | book2 |
| 3  | book3 |

so, when I will delete books with id 2, then the controller always receives id 3. and when i tries to delete id 1, that remains the case, the controller gain id 3 (always the last record)

this view blade

@foreach($datas as $data)
<a href="# {{ $data->id }}"
   onclick="event.preventDefault();
   document.getElementById('remove-form').submit();"
  rel="tooltip" title="Hapus" class="btn btn-danger">
</a>

<form id="remove-form" action="{{ url('/dashboard/book/delete/'. $data->id) }}" method="post">
    {{ csrf_field() }}
    {{ method_field('DELETE') }}
</form>
@endforeach

this route

Route::group(['namespace' => 'Backend'], function ()
{
   Route::resource('/dashboard/book', 'BookController'); //
   Route::delete('/dashboard/book/delete/{id}', 'BookController@destroy');
}

this controller

public function index()
    {
        $datas = Book::all();
        return view('backend.bookview', compact('datas'));
    }
public function destroy($id)
{
    Book::where('id', $id)->delete();
    return redirect('/dashboard/book')->with('ok', translate('back/book.destroyed'));
}

2 Answers 2

1

This is happening because document.getElementById('remove-form').submit() always selects the last form which has id => 3.

So change it to following code and it will work:

@foreach($datas as $data)
    <form method="POST" action="{{ url('/dashboard/book/delete/'. $data->id) }}"  style="display: inline-block;">
        {{ csrf_field() }}
        {{ method_field('DELETE') }}
        <button onclick="return confirm('Do you really want to delete this item?');" type="submit" class="btn btn-danger" data-original-title="Delete Item" data-toggle="tooltip" data-placement="top" title="">
            Delete
        </button>
    </form>
@endforeach
Sign up to request clarification or add additional context in comments.

Comments

1

UPDATE

You can't have multiple id remove-form on a single html page. Instead use classes like this:

@foreach($datas as $data)
    <div class="delete-block">
        <a href="# {{ $data->id }}"
          rel="tooltip" title="Hapus" class="btn btn-danger delete-btn">
        </a>

        <form class="remove-form" action="{{ url('/dashboard/book/delete/'. $data->id) }}" method="post">
            {{ csrf_field() }}
            {{ method_field('DELETE') }}
        </form>
    </div>
@endforeach

and your script should go as:

$(function() {

    $('.delete-btn').on('click', function(e) {
        $(this).closest('.delete-block').find('form').submit();
    });

});

$(function() {

  $('.delete-btn').on('click', function(e) {
    e.preventDefault();
  	console.log($(this).closest('.delete-block').find('form').attr('action'));
    // $(this).closest('.delete-block').find('form').submit();
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="delete-block">
  <a href="#1" rel="tooltip" title="Hapus" class="btn btn-danger delete-btn">
    Delete 1
  </a>

  <form id="remove-form" action="/dashboard/book/delete/1" method="post">
      <input type="hidden">
  </form>
</div>

<div class="delete-block">
  <a href="#2" rel="tooltip" title="Hapus" class="btn btn-danger delete-btn">
    Delete 2
  </a>

  <form id="remove-form" action="/dashboard/book/delete/2" method="post">
      <input type="hidden">
  </form>
</div>

<div class="delete-block">
  <a href="#3" rel="tooltip" title="Hapus" class="btn btn-danger delete-btn">
    Delete 3
  </a>

  <form id="remove-form" action="/dashboard/book/delete/3" method="post">
      <input type="hidden">
  </form>
</div>

Inside Controller

You should delete the book by fetching the single record by id from database, like this:

public function destroy($id)
{
  $book = Book::find($id);
  if($book) {
    $book->delete();
    return redirect('/dashboard/book')->with('ok', translate('back/book.destroyed'));
  }
  // return error response - book deletion failed!
  return redirect('/dashboard/book')->with('Error', translate('back/book.destroyed.error'));
}

Use find() to fetch single record via primary key, from database.

However, if you know the primary key of the model, you may delete the model without retrieving it. To do so, call the destroy method:

Book::destroy(1); // Can pass single primary key
Book::destroy([1, 2, 3]); // Can pass an array of primary keys
Book::destroy(1, 2, 3); // Can pass multiple primary keys via arguments

See more about Deleting Models in Laravel

Hope this helps!

4 Comments

but id parameter that I pass on the route when it came to controller the params id turns into the id of the last record
dashboard/book/delete/2 going to destroy in controller receives destroy(3) isnt 2, I feel confused sir
Wait, I am updating my answer!
yes, I have a check, sir thanks for your attention :-D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.