Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hjortur17's avatar

Delete function not working

Hi, I'm trying to delete thread with onClick function. But I get MethodNotAllowedHttpException error

My code is:

<a class="is-trash" onclick="document.getElementById('deleteForm').submit();">
    <i class="fal fa-trash"></i>
</a>
<form method="POST" action="{{ route('thread.delete', $thread) }}" id="deleteForm">
    @csrf
    @method('DELETE')
</form>

And the controller looks like this:

public function destroy(Thread $thread)
{
    $thread->delete();
    return back();
}
0 likes
32 replies
hjortur17's avatar
Route::delete('/stjornbord/frettir/eyda/{thread}', 'ThreadsController@destroy')->name('thread.delete');
devk's avatar

Can you make sure that the URL is correct after clicking on the button?

What exactly does your $thread variable hold in here:

<form method="POST" action="{{ route('thread.delete', $thread) }}" id="deleteForm">
rin4ik's avatar

with this?

<form method="POST" action="{{ route('thread.delete', $thread) }}" id="deleteForm">
    {{csrf_field()}}
  <input type="hidden" name="_method" value="DELETE">
</form>

and dd

Route::delete('/stjornbord/frettir/eyda/{thread}', function(){
dd('ok');
})->name('thread.delete');
1 like
devk's avatar

No, I meant, just check the URL bar in your browser after clicking on the Delete button - that it's actuall /stjornbord/frettir/eyda/{id}

As for the $thread variable, I meant in the view, like this:

<form method="POST" action="{{ route('thread.delete', $thread) }}" id="deleteForm">
  {{ dd($thread) }}
  {{csrf_field()}}
  <input type="hidden" name="_method" value="DELETE">
</form>
1 like
hjortur17's avatar

The URL is /stjornbord/frettir/eyda/1 the ID of the thread

ejdelmonico's avatar

Is the row actually deleted from the DB? If so, then change the back() to a redirect because the page no longer exists.

2 likes
hjortur17's avatar

I tried @rin4ik idea about making function inside web.php but that did work

It will not display ok

devk's avatar

If the page doesn't exist it would throw 404 instead.

The code seems about right to me. Next thing I would do is check php artisan route:list and make sure you can see the URL there.

Or put a Submit button within the form and click it, see if it works that way.

rin4ik's avatar

I'm not sure but try this

<a href="{{ route('thread.delete', $thread) }}" class="is-trash" onclick="event.preventDefault(); document.getElementById('deleteForm').submit();">
<i class="fal fa-trash"></i>
</a>
<form method="POST" action="{{ route('thread.delete', $thread) }}" id="deleteForm">
    @csrf
    @method('DELETE')
</form>

and if it's successful don't redirect back because your thread already deleted

hjortur17's avatar

@devk this is the outcome:

| DELETE | stjornbord/frettir/eyda/{thread} | thread.delete | Closure | web

devk's avatar
devk
Best Answer
Level 3

Can you try this, just temporarily, but to make sure that a) it's submitting the correct form b) the JS is not doing anything funny:

<form method="POST" action="{{ route('thread.delete', $thread) }}" id="deleteForm">
    @csrf
    @method('DELETE')
    <button type="submit">Submit</button>
</form>

And click the Submit button. See if it works that way maybe.

1 like
hjortur17's avatar

Nope the buttons did not work (When clicked nothing happend)

devk's avatar

What do you mean nothing happened? Unless you have some type of e.preventDefault() on the form submit event, something should happen here.

You can try replacing it with this:

<input type="submit" value="Submit">

But if nothing happens when you click on it, and you don't explicitly prevent the default submit event anywhere, there's something wrong.

hjortur17's avatar

The DD worked (I did dd($thread) same as @devk suggested), and the result was:

#attributes: array:8 [▼
    "id" => 2
    "slug" => "Title 2"
    "user_id" => 1
    "title" => "Title 2"
    "body" => "https://nfs.test/stjornbord/frettir/eyda/1aas ads asd"
    "image" => "frettir/yeIIaCv1f9azslcsYwSDjnLVCGtu1Zb4kQsXG7gs.jpeg"
    "created_at" => "2018-04-24 08:59:59"
    "updated_at" => "2018-04-24 08:59:59"
  ]
rin4ik's avatar

@hjortur17 now it should work try please this

Route::delete('/stjornbord/frettir/eyda/{thread}', 'ThreadsController@destroy')->name('thread.delete');
public function destroy(Thread $thread)
{
    $thread->delete();
    return back();
}
devk's avatar

Change your route to this:

<form method="POST" action="{{ route('thread.delete', $thread->id) }}" id="deleteForm">
Cronix's avatar

@rin4ik return back(); will probably cause an error since you just deleted the item you're returning to.

Cronix's avatar

And is it actually deleting the things you are telling it to in the db, but just causing an error on top of it? Or not deleting and causing an error?

rin4ik's avatar

@Cronix yes I forgot to remove redirect back() . thanks

and if it's successful don't redirect back because your thread already deleted

hjortur17's avatar

I changed that to

return redirect('/stjornbord/frettir');
Cronix's avatar

try running php artisan view:clear.

<form method="POST" action="{{ route('thread.delete', $thread) }}" id="deleteForm">
    @csrf
    @method('DELETE')
    <button type="submit">Submit</button>
</form>

this should work with Laravel 5.6.x and actually submit. You say nothing happens when you click submit. I don't know what else could be going on unless you have some other javascript going on that we don't know about.

Next

Please or to participate in this conversation.