I am trying to hit a route I made to make a delete HTTP request in laravel view when user clicks on 'Delete' button, but it won't work. I've read it should be done with forms in laravel.
Here is my code:
<form action="/admin/pages/delete/{{ $section->id }}" method="post">
{{ method_field('delete') }}
<button class="btn btn-sm" type="submit">Delete</button>
</form>
What is a proper way to handle this?
It shows me an error in the console, Bootbox: 'please specify a message' whenever I click on the button.
Route definition inside admin group:
Route::delete('/pages/delete/{id}', 'PagesController#delete')->name('pages.delete');
I believe you are missing the csrf token in the form.
You can add
{{ csrf_field() }}
just after your form starts.
Visit this link for knowing more about csrf
You must add the the CSRF Field because all form submission must past through the VerifyCsrfToken middleware before the request be procede by the controller
{{ csrf_field() }} // add this before or after the {{ method_field() }}
Related
Hey guys i have an error when i submit the delete it says that i'm not sending the id to the route i think,
that's the code to send the id to the route (and sorry for my english),
<form id="del_type" action="{{ route('admin.event.destroy',$event->id)}}" method="post">
{!! method_field('delete') !!}
{{ csrf_field() }}
<button class="btn btn-danger" type="submit" id="del_id">Supprimer</button>
it's wierd cause when i submit it's deleting the element from the database but with this error and in the URI i can see the id
take a look at the routes (All route names are prefixed with 'admin.')
Route::delete('evenment/event/{id}','EventController#destroy')->name('event.destroy');
change route to
admin.event.destroy
Change action to this and try
{{ route('admin.event.destroy', ['id' => $event->id])}}
I'm using Laravel and I'm trying to achieve a delete function that takes care of deleting the selected task the user made.
I was wondering how can I achieve this using routes?
<button class="btn btn-outline-light">Edit</button>
<button class="btn btn-outline-light">Delete</button>
At this moment my delete route takes me to the same page as what the edit route does.
My delete function:
public function destroy(Task $task)
{
$task->delete();
return redirect('/tasks')->with('success', 'Task removed');
}
I got a working delete function using the Laravel forms:
{!! Form::open(['action' => ['TasksController#destroy', $task->id], 'method' => 'POST', 'class'=> 'float-right']) !!}
{{Form::hidden('_method','DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-outline-danger'])}}
{!! Form::close() !!}
Is it worth to consider another method of taking care of my deletes or should I stick with using the Laravel forms?
That's because of CSRF protection. You may think using an ajax request.
You can find an example in this post.
How to delete record in laravel 5.3 using ajax request?
When you press the delete link, you'll be basically making a GET request to the tasks.destroy route, which is not what you want. You can either create a form and submit it on click (the laravel logout btn does something similar)
<a href="#"
onclick="event.preventDefault(); document.getElementById('task-destroy-form').submit();">
Delete task
</a>
<form id="task-destroy-form" action="{{ route("tasks.destroy", ["id" => $task->id]) }}" method="POST" style="display: none;">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE">
</form>
or make an ajax call that will send the id and CSRF parameters to the route.
i was trying to follow this tutorial on Laravel, even though most of the code & directories there were outdated, i managed to make the "Add Task" and also The Form to show up after a few errors.
now the problem, the delete button.
when i click on it, it shows a "MethodNotAllowedHttpException".
i changed the source code to match the newest version of Laravel.
my form (current version) :
<form action="{{ url('/task/'.$task->id) }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-danger">
<i class="fa fa-btn fa-trash"></i>Delete
</button>
</form>
my route :
Route::delete('/task/{id}', function ($id) {
Task::findOrFail($id)->delete();
return redirect('/');
});
i've been trying to fix this for 4 hours now, changing the methods of my route and form; but to no avail.
this is my first question on this site, sorry if there's something wrong in this question.
thanks~
edit:
to further help the effort, here's the complete error log
Error log, in Google Chrome
Change
<form action="{{ url('task/'.$task->id) }}" method="DELETE">
to
<form action="{{ url('task/'.$task->id) }}" method="POST">
Because form method DELETE does not exist, Laravel just "overwrites" this method with the hidden input "method" (you can place this input using "{{ method_field('DELETE') }}").
Form dosent support method Delete or Put ... It support only get and post methods, if You want implement delete in laravel this article will help you
link
Answer :
Turns out the problem lies in the Database-table itself, let me explain :
i was referring to a 'tasks' table that is referred as 'task' in code (no problem)
BUT, i was referring to a column called "ID" in my table as "id" in my code, creating the error (a rookie mistake).
thanks to #Autista_z for the pointer, and everyone else below for the guidance!
I'm using resource controller in Laravel 5.3 and I'm having problem with deleting a record. I would like to use simple HTML code and I know that I have to add a hidden method input to make it work.
My code is very simple:
<form action="{{ url('/task', $task->id) }}">
{{ method_field('DELETE') }}
<input type="submit" value="Delete" />
</form>
After clicking submit app redirects to blank page - it doesn't go to destroy function in controller. I don't have any idea, why it's not working. I'm not using facades, is it necessary in operation like this? I'll be very glad for every tip, thank you.
You're most likely running into a TokenMismatchException. Laravel considers the DELETE method a "writable" method, so it expects a CSRF token.
You can either add a CSRF token to your form, or, if appropriate, you can add your URI to the except array in your app/Http/Middleware/VerifyCsrfToken.php file.
To add the token to your form:
<form action="{{ url('/task', $task->id) }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<input type="submit" value="Delete" />
</form>
How can I place a token in laravel, I am getting a token error? Thank you for your collaboration.
I'm guessing the reason way Jeffery's code works is because the version of Laravel doesn't have default csrf protection, so if u want to make your form works, you have to manually add :
<input type="hidden" name="_token" value="{{ csrf_token() }}">
or create the form with the form helper:
{{ Form::open(array('url' => 'foo/bar')) }}
//
{{ Form::close() }}
which will automatically generate the hidden input tag of csrf token