Search code examples
phplaravelcrud

My route to delete is redirecting me to show.blade instead of destroying the data with corresponding id


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.

Solution

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