Home > Software design >  How do I call the laravel destroy function through an html form and javascript?
How do I call the laravel destroy function through an html form and javascript?

Time:11-27

(I'm just a beginner!!)

Controller:

public function destroy(Pessoa $pessoa)
{
    $pessoa->delete();
    return redirect("http://localhost:5500/index.html");
}

I can use post with

 <form action="http://127.0.0.1:8000/api/pessoas" method="POST">

But how can I do it with the delete method?

CodePudding user response:

You can add csrf and method via blade

<form action="http://127.0.0.1:8000/api/pessoas" method="post">
   @csrf
   @method('DELETE')
</form>

Alternatively, using Javascript Fetch:

fetch("http://127.0.0.1:8000/api/pessoas", {
    method: "DELETE",
    headers: {
        'Content-Type': 'application/json'
    },
})
  • Related