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!
Related
I'm using Laravel 5.8 for my project and in this project, I have added this route:
Route::post('course_admin/{id}','CourseController#AcceptWalletCourse')->name('accept.walletCourse');
And here is the form:
<form method="POST" action="{{ route('accept.walletCourse',['id'=>last(request()->segments())]) }}">
#csrf
Are you sure?
<button type="submit" class="btn btn-success">Yes</button>
</form>
And at the Controller, I added this:
dd($request);
But I don't know why nothing appears as result. I mean the page just refreshes without showing any error or any dd result.
So what's going wrong here? How can I solve this issue?
Try to simplify everything:
<form method="POST" action="{{ route('accept.walletCourse',['id'=> 12]) }}">
#csrf
Are you sure?
<button type="submit" class="btn btn-success">Yes</button>
</form>
In your controller:
public function AcceptWalletCourse($id)
{
dd($id); //should print 12
}
Once you are at this point, you can start adding validations, policies or dynamic parameters in your form (for instance last(request()->segments())) .
By proceeding step by step, you will quickly find where is the problem.
Try php artisan clear-compiled composer dump-autoload php artisan optimize
After finding out I've been writing my routes slightly wrong I decided to fix them. The routes originally looked like this:
Route::get('/deleteImage/{id}', 'ArtworkController#deleteImage')->name('deleteImage');
Route::get('/deleteCategory/{id}', 'CategoryController#deleteCategory')->name('deleteCategory');
Route::patch('/profile/{id}/update', 'UsersController#updateProfile')->name('updateProfile');
And I changed them to:
Route::delete('/image/{id}', 'ArtworkController#deleteImage')->name('deleteImage');
Route::delete('/category/{id}', 'CategoryController#deleteCategory')->name('deleteCategory');
Route::patch('/profile/{id}', 'UsersController#updateProfile')->name('updateProfile');
Sadly, as soon as I changed Route::get('/deleteImage/{id}' to Route::delete('/image/{id}' something went wrong.
This is the <a> tag that leads to the deleteImage route:
<a class='placeholderDelete' href='{{ route('deleteImage', ['image_id' => $image->id]) }}'>Delete Image</a>
Before making the changes the image would get deleted after clicking that anchor element, however, after the changes the page just refreshes and nothing happens. The image doesn't get deleted from my database and storage.
I'm not sure how to troubleshoot as I'm not getting any errors or anything like that.
DELETE routes need to be handled by a form that uses POST. Normal links (anchor tags) visit a page using a GET request. You will need to use a form. Here is an example.
<form id="delete-form" class="form" role="form" method="POST" action="{{route('deleteImage', $image->id)}}">
{{ csrf_field() }}
<input name="_method" type="hidden" value="DELETE">
<button type="submit">Delete</button>
</form>
If you want to use a regular link to do the deletion, you could hide the form and use javascript to submit it.
Delete Image
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() }}
I'm having a bit of an issue when it comes to updating a form and and having an file input. Here is what I am working with.
I have a form in laravel 5.1 which has a post method and a hidden 'Patch' method. This works as is should updating the fields that are in the form. However, when it introduce:
<input type="file" id="profile_picture" name="image_url" />
into the form, i get a:
MethodNotAllowedHttpException in RouteCollection.php line 218:
laravel error. I have tried changing the
<input type='hidden' name='_method' value='PATCH'>
to PUT and it still doesnt like it.
My form looks like this:
<form action='{{url("profiles/$user->id")}}' method="post" class="form-horizontal" enctype="multipart/form-data">
route resource looks like this:
Route::resource('profiles', 'ProfilesController');
I can't figure out what I am missing here...Any help is much appreciated.
I believe it has to do with the exact route you are typing out in the "action" parameter matching up with the profile controller's update method.
Try changing
action'{{url("profiles/$user->id")}}'
to
action='{{ route("profiles.update", $user->id) }}'
Additionally, you could use the Laravel Collective HTML package to simply opening and closing of forms.
Also for POST Request types, you need to send the CSRF token along with your form data. If you are using laravel blade template in your view, you may use
{{ csrf_field() }}
which translates to
<input type="hidden" name="_token" value={{ csrf_token() }}
Please refer the documentation for this.
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>