Methods (PUT, UPDATE, DELETE) that are not supported in some routes in Laravel - laravel

I'm having some problems about some routes that don't support certain methods.
// The PUT method is not supported for this route. Supported methods: POST.
Route::post('/action_create_hunter', [HunterController::class, 'store'])->name('action_create_hunter.store');
<form action="{{ url("action_create_hunter") }}" method="POST">
#csrf
.....
// The UPDATE method is not supported for this route. Supported methods: POST.
Route::post('/action_update_hunter/{id}', [HunterController::class, 'update'])->name('action_update_hunter.update');
<form action="{{ url("action_update_hunter/$hunter->id") }}" method="POST">
#csrf
.....
// The GET method is not supported for this route. Supported methods: DELETE.
Route::delete('/delete_hunter/{id}', [UserController::class, 'destroy'])->name('action_delete_hunter.destroy');
<i class="fa fa-trash"></i> Delete

You should define a hidden input with the Method name in this case , and for Laravel you can use #method , for example , If you want this be Put Method
The Route Will be
Route::put('/action_update_hunter/{id}', [HunterController::class, 'update'])->name('action_update_hunter.update');
And The form :
<form action="{{ url("action_update_hunter/$hunter->id") }}" method="POST">
#csrf
#method('put')
....
</form>
it's called Form Method Spoofing , check it in laravel docs For more information
https://laravel.com/docs/9.x/routing#form-method-spoofing

Related

The POST method is not supported for this route. Supported methods: PUT. LARAVEL

I would like to send data with PUT method
However, this error happens.
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The POST method is not supported for this route. Supported methods: PUT.
I don't really understand why my code is not correct.
web.php
Route::get('/', function () {
return redirect('/home');
});
Auth::routes();
Route::put('/save_data', 'AbcController#saveData')->name('save_data');
view.blade.php
<form action="{{route('save_data')}}" method="POST">
#method('PUT')
#csrf
<input type = "hidden" name = "type" value ='stack' >
<div>
<button>post</button>
</div>
</form>
when it is changed
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
instead of
#method('PUT')
#csrf
It works well.
make sure to check First Another Route with Same Name
You can make POST route and dont need to put this after form tag #method('PUT')
CHange the Placement of the route before this Auth::routes();
use save_data in route instead of /save_data.
use {{url('save_data')}} in action instead of {{route('save_data')}}.
If you insist on using PUT you can change the form action to POST and
add a hidden method_field that has a value PUTand a hidden csrf field
(if you are using blade then you just need to add #csrf_field and {{
method_field('PUT') }}). This way the form would accept the request.
You can simply change the route and form method to POST. It will work
just fine since you are the one defining the route and not using the
resource group
after all this run artisan command
php artisan route:clear
Change:
<button>post</button>
to:
<button type="submit">post</button>

Route not defined in form action in laravel 7

View Form
Form
Routes
Routes
Route list
Route list
Controller
Reg controller
ERROR
Route [] not defined.
if you use resource
{{ route('student.store') }}
if you use ->name("student")
{{ route('student') }}
you have to read carefully the laravel documentation for routes
The form action attribute specifies where to send the form-data when a form is submitted.
so you simply direct submitted data as follow :
<form method="POST" action="student/store">
provided that your route like :
Route::post("student/store", "RegStudentsController#store");
or if you want to use the route:
Route::post("/student", "RegStudentsController#store")->name("student");
<form method="POST" action="{{ route('student') }}">
Update
as per your updated screen shoots the solution will be doing something like:
<form method="POST" action="{{ url('/student') }}">

use patch method to update model in laravel

i want to update a customer using patch methodn here is my route
Route::patch('/customers/updateCustomer', 'CustomerController#update')
->name('customers.update');
and here is my form :
<form method="POST" action="{{ route('customers.update') }}">
#csrf
#method('PATCH')
how to pass the id of the customer , the update method requires two parameters, when the first one is the data from the from , the second should be the id of the customer
You should send the customer id with the route() helper function.
Make sure to send the customer object during the form rendering.
<form method="POST" action="{{ route('customers.update', $customer['id']) }}">
#csrf
#method('PATCH')
And modify the route slightly.
Route::patch('/customers/updateCustomer/{customerId}', 'CustomerController#update')->name('customers.update');
add customer id to the route like
Route::patch('/customers/updateCustomer/{id}', 'CustomerController#update')->name('customers.update');
and then your form action should be like
<form method="POST" action="{{ route('customers.update',$customer->id) }}">
you have to send the customer object from where you are returning to the form.
and finally your update function
public function update(Request $request, $id)
{
//your things to do
}
You have to use like this
<form method="POST" action="{{ route('customers.update',['id'=>$customer->id]) }}">
And your route should be
Route::patch('/customers/updateCustomer/{id}', 'CustomerController#update')->name('customers.update');
And your controller should be
public function update(Request $request, $id){
//your code
}

Laravel verfication.resend - The GET method is not supported for this route. Supported methods: POST

Odd question here. Im using the default Auth::routes(['verify' => true]); In Laravel 6. So I register ( Custom registration form ) and all works fine ( added to database etc ) then I am taken to the verification page where it has an email link to resend. When I click this I get:
The GET method is not supported for this route. Supported methods: POST.
The View has this named routed in the link route('verification.resend')
As you can see here. Verify resend is a POST route. So GET method is not allowed. So it should be a form Post instead.
If you are using blade something like this will get you there.
<form method="POST" action="{{ route('verification.resend')) }}">
</form>
Because in laravel 6+ they added this route as a post so you can do it by below code
<a onclick="event.preventDefault(); document.getElementById('email-form').submit();">{{ __('click here to request another') }}
</a>.
<form id="email-form" action="{{ route('verification.resend') }}" method="POST" style="display: none;">
#csrf
</form>

Laravel MethodNotAllowedHttpException No message

I'm getting this error after posting the data.
Route;
Route::post('/classified/location', 'ClassifiedController#locationPost')->name('location-post');
Form;
<form class="form" method="post" action="/classified/location">
#csrf
Please check your route file to make sure that you haven't defined the same route twice, in that case later will replace the prior so make sure route definition is unique and exactly once.
Also if you anyway are naming your routes then use the name itself to target it so use:
<form class="form" method="post" action="{{ route('location-post') }}">
Also, make sure you don't define two or more routes with the same name.
Your form tag like that:
<form class="form" method="post" action="{{route('location-post')}}">
OR
<form class="form" method="post" action="{{url('/classified/location')}}">

Resources