Can’t delete data in Laravels

I will delete data by id but, the button is not working when I cick to delete.
I need you help.

Here’s my view

 @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

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

here’s my route
Route::delete('/deleteBook/{id}','<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b29040400182804051f190407070e192b0f0e070e1f0e29040400">[email protected]</a>')->name('deletebook');

I want to use button with icon like that, and i want to delete the data using that button, but when i clicked its not working. Do you have any solution?
Thank you

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try this code

At the blade (view)

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

Note: The form is a display block you can change to inline by adding this class d-inline

Route

Route::delete('/deleteBook/{book}','<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3f1dcdcd8c0f0dcddc7c1dcdfdfd6c1f3d7d6dfd6c7d6f1dcdcd8">[email protected]</a>')->name('deletebook');

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

Try it, and tell me if is everything alright.

Happy Coding.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x