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();
}
Route::delete('/stjornbord/frettir/eyda/{thread}', 'ThreadsController@destroy')->name('thread.delete');
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">
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');
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>
The URL is /stjornbord/frettir/eyda/1 the ID of the thread
Is the row actually deleted from the DB? If so, then change the back() to a redirect because the page no longer exists.
I tried @rin4ik idea about making function inside web.php but that did work
It will not display ok
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.
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
@devk this is the outcome:
| DELETE | stjornbord/frettir/eyda/{thread} | thread.delete | Closure | web
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.
Nope the buttons did not work (When clicked nothing happend)
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.
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"
]
@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();
}
Change your route to this:
<form method="POST" action="{{ route('thread.delete', $thread->id) }}" id="deleteForm">
@rin4ik return back(); will probably cause an error since you just deleted the item you're returning to.
Still getting the same error
@hjortur17 what's the exact version of laravel are you using? (php artisan -V)
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?
@Cronix yes I forgot to remove redirect back() . thanks
and if it's successful don't redirect back because your thread already deleted
I changed that to
return redirect('/stjornbord/frettir');
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.
Please sign in or create an account to participate in this conversation.