无法在 Laravel 中删除数据

Can't delete data in Laravels

我会按id删除数据,但是当我点击删除时按钮不起作用。 我需要你的帮助。

这是我的看法

 @foreach ($books as $books)
           <tr>
              <td>{{$books->id}}</td>
              <td>{{$books->name}}</td>
              <td>{{$books->year}}</td>
              <td>{{$books->publisher}}</td>
              <td>
                  <button type="button" class="btn btn-info" title="details"><i class="fa fa-info" ></i></button>
                  <button type="button" class="btn btn-warning" title="edit"><i class="fa fa-pencil" ></i></button>
                  <button type="button" class="btn btn-danger" title="delete"><i class="fa fa-trash"></i><a href ="{{ route('deletebook',[$books->id])}}" onclick="return confirm('Are you sure to delete this book?')"></a></button>
             </td>
          </tr>
@endforeach

这是我的控制器

 public function deleteBook($id) {
   DB::table('books')->where('id', $id)->delete();
   return redirect('home')->with('status', 'DELETE succesfully'); }

这是我的路线

Route::delete('/deleteBook/{id}','BooksController@deleteBook')->name('deletebook');

我想使用带有这样图标的按钮,我想使用该按钮删除数据,但是当我点击它时它不起作用。你有什么解决办法吗? 谢谢

试试这个代码

在blade(查看)

<form action="{{ route('deletebook',[$books->id])}}" method="POST">
  @csrf
  @method('delete')
  <button type="submit" onclick="return confirm('Are you sure to delete this book?')" class="btn btn-danger"><i class="fa fa-trash"></button>
</form>

注意:表单是一个显示块,您可以通过添加此 class d-inline

更改为内联

路线

Route::delete('/deleteBook/{book}','BooksController@deleteBook')->name('deletebook');

控制器

//Add Book model
public function deleteBook(Book $book) {
   $book->delete();
   return redirect('home')->with('status', 'DELETE succesfully'); 
}

试一试,告诉我是否一切正常。

编码愉快。