Delete function not working

Published 5 months ago by hjortur17

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();
}
Best Answer (As Selected By hjortur17)
devk

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.

rin4ik
rin4ik
5 months ago (266,140 XP)

```

your code

```

hjortur17

Done, sorry about that

rin4ik
rin4ik
5 months ago (266,140 XP)

show your route please

hjortur17
Route::delete('/stjornbord/frettir/eyda/{thread}', '[email protected]')->name('thread.delete');
devk
devk
5 months ago (9,880 XP)

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
rin4ik
5 months ago (266,140 XP)

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');
devk
devk
5 months ago (9,880 XP)

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

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

ejdelmonico

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

rin4ik
rin4ik
5 months ago (266,140 XP)

@ejdelmonico correct :)

hjortur17

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

It will not display ok

devk
devk
5 months ago (9,880 XP)

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
rin4ik
5 months ago (266,140 XP)

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

@devk this is the outcome:

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

hjortur17

@rin4ik didn't work :/

Please sign in or create an account to participate in this conversation.