skip to Main Content

I am trying to delete a particular data with corresponding id but it redirects me to the Show page although the route is directing it to destroy.

My index.blade

<td><form class="action"><button type="submit" formaction="{{route('products.show',$product->id)}}">Show</button></form></td>

<td><form class="action"><button type="submit"  formaction="{{route('products.edit',$product->id)}}">Edit</button></form></td>

<td><form class="delete" action="{{ route('products.destroy',$product->id) }}">
  @csrf
  @method('DELETE')
  <button>Delete</button>
  </form></td>

My Controller for destroy

public function destroy(Product $product)
    {
        $product->delete();
        return redirect()->route('products.index')->with('success','Data deleted successfully!!!');
    }

My Controller for show

public function show(Product $product)
    {
        return view('products.show',compact('product'));
    }

My controller for index

    {
        $products = Product::latest()->paginate(5);
        return view('products.index',compact('products'));
    }````

Route
````Route::resource('/products',ProductController::class);````


I tried using <a> link tag also but it does the same and also the data gets deleted perfectly fine when I delete it from database manually.

2

Answers


  1. Your delete button form does not have a button type="submit" also it doesn’t have method="post" try below code.

    <form class="delete" action="{{ route('products.destroy',$product->id) }}" 
    method="POST">
        @csrf
        @method('DELETE')
        <button type="submit">Delete</button>
    </form>
    

    I hope this will solve your issue.

    Login or Signup to reply.
  2. index.blade 
    <form class="delete" action="{{ route('products.destroy',$product->id) }}" 
    method="POST">
       @csrf
       @method('DELETE')
       <button type="submit">Delete</button>
    </form>
    
    controller
    
    public function destroy(string $id)
    {
        $product= Product::findOrFail($id);
        $product->delete();
        return redirect()->route('products.index')->with('success','Data 
        deleted successfully!!!');
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search