我发现了一个不同的错误。我试图通过id删除书籍。 我有数据,例如
--------------
| id | name |
--------------
| 1 | book1 |
| 2 | book2 |
| 3 | book3 |
所以,当我删除id为2的书籍时,控制器总是会收到id 3。 当我试图删除id 1时,情况仍然如此,控制器获得id 3(总是最后一条记录)
此视图刀片
@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
这条路线
Route::group(['namespace' => 'Backend'], function ()
{
Route::resource('/dashboard/book', 'BookController'); //
Route::delete('/dashboard/book/delete/{id}', 'BookController@destroy');
}
这个控制器
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'));
}
答案 0 :(得分:1)
<强>更新强>
您无法在单个html页面上拥有多个ID remove-form
。而是使用这样的类:
@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
你的脚本应该是:
$(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();
});
});
&#13;
<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>
&#13;
内部控制器
您应该通过从数据库中获取id的单个记录来删除该书,如下所示:
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'));
}
使用find()
从数据库中通过primary key
获取单个记录。
但是,如果您知道模型的主键,则可以删除模型而不检索它。为此,请调用destroy方法:
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
希望这有帮助!
答案 1 :(得分:1)
这种情况正在发生,因为document.getElementById('remove-form').submit()
始终选择具有id
=&gt;的最后一个表单3
。
因此,将其更改为以下代码,它将起作用:
@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