Laravel form won't PATCH, only POST - nested RESTfull Controllers, MethodNotAllowedHttpException - laravel

I am trying to allow users to edit their playlist. However, whenever I try to execute the PATCH request, I get the MethodNotAllowedHttpException error. (it is expecting a POST)
I have set up RESTful Resource Controllers:
Routes.php:
Route::resource('users', 'UsersController');
Route::resource('users.playlists', 'PlaylistsController');
This should give me access to: (as displayed through php artisan routes)
URI | Name | Action
PATCH users/{users}/playlists/{playlists} | users.playlists.update | PlaylistsController#update
However, when I try to execute the following form, I get the MethodNotAllowedHttpException error:
/users/testuser/playlists/1/edit
{{ Form::open(['route' => ['users.playlists.update', $playlist->id], 'method' => 'PATCH' ]) }}
{{ Form::text('title', $playlist->title) }}
{{ Form::close() }}
If I remove 'method'=> 'PATCH' I don't get an error, but it executes my public function store() and not my public function update()

In Laravel 5 and up:
<form method="POST" action="patchlink">
#method('patch')
. . .
</form>

Write {!! method_field('patch') !!} after form:
<form method="POST" action="patchlink">
{!! method_field('patch') !!}
. . .
</form>
Official documentation for helper function method_field()

Since html forms support only GET and POST you need to add an extra hidden field
to the form called _method in order to simulate a PATCH request
<input type="hidden" name="_method" value="PATCH">

As suggested by #Michael A in the comment above, send it as a POST
<form method="POST" action="patchlink">
<input type="hidden" name="_method" value="PATCH">
Worked for me.

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>

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: REJECT ROUTE not defined but exists in web.php

I have a reject function in my Calendar controller but whenever I redirect to the view page it displays an error saying my route is not defined.
I've tried rearranging and renaming my route but it's still displaying the error.
Here is my form:
{!! Form::open(['url' => route('therapist.reject.appointment', $bookingRequest), 'method' => 'delete', 'onsubmit' => 'javascript:return confirm("Are you sure?")']) !!}
<button type="submit" class="btn btn-warning btn-block">Reject this appointment</button>
{{csrf_field()}}
{!! Form::close() !!}
Here are my routes. The other routes displayed are working perfectly:
Route::get('therapist-calendar/{bookingRequest}', 'TherapistCalander')->name('therapist.calendar');
Route::post('therapist-calendar/{bookingRequest}',
'TherapistCalander#saveAppointment')->name('therapist.book.appointment');
Route::patch('therapist-calendar/{bookingRequest}',
'TherapistCalander#finishedAppointment')->name('therapist.finish.appointment');
Route::delete('therapist-calendar/{bookingRequest}',
'TherapistCalander#rejectAppointment')->name('therapist.reject.appointment');
Route::delete('therapist-calendar/{bookingRequest}',
'TherapistCalander#cancelAppointment')->name('therapist.cancel.appointment');
And lastly, my function:
public function rejectAppointment(Request $request, BookingRequest $bookingRequest)
{
$bookingRequest->reject();
return redirect()->back()->with('rejectStatus', true);
}
The view page where this button belongs should be able to display the buttons for rejecting and finishing, alongside the calendar view.
EDIT
Follow up question: Is it possibly because the routes are similar to one another? If so, how do I fix this?
Try to change the Reject and Cancel the url string because it is similar.
Route::delete(
'therapist-calendar/{bookingRequest}/delete',
'TherapistCalander#rejectAppointment'
)->name('therapist.reject.appointment');
Route::delete(
'therapist-calendar/{bookingRequest}',
'TherapistCalander#cancelAppointment'
)->name('therapist.cancel.appointment');
Change your code to
{!! Form::open(['url' => route('therapist.reject.appointment', ['bookingRequest' => $bookingRequest]), 'method' => 'delete', 'onsubmit' => 'javascript:return confirm("Are you sure?")']) !!}
{{csrf_field()}}
<button type="submit" class="btn btn-warning btn-block">Reject this appointment</button>
{!! Form::close() !!}
The route parameters are passed as an array and that should work fine. Refer doc
Can you try this code
<form action="{{ route('therapist.reject.appointment', ['bookingRequest' => $bookingRequest]) }}" method="POST">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-warning btn-block">Reject this appointment</button>
</form>
UPDATE
PROBLEM FIXED
I realized since they have similar links, web.php found it confusing so it did not read this route.
That is why I changed my route from:
Route::delete('therapist-calendar/{bookingRequest}',
'TherapistCalander#rejectAppointment')->name('therapist.reject.appointment');
To this:
Route::delete('doReject/{bookingRequest}',
'TherapistCalander#rejectAppointment')->name('therapist.reject.appointment');

Laravel 5.4 method not allow on patch

I'm working on updating the data through a PATCH form, the form is working on localhost, but it is not working on server, i have check the route list the route i create is using the PATCH method also, but laravel return me a method not allow exception, here is my code:
Controller:
public function registercert (Request $request, $id) {
// return $request->all();
$user = User::findOrFail($id);
}
Route:
Route::patch('admin/user/registercert/{id}', ['as'=>'registercert', 'uses'=>'admin\AdminUserController#registercert']);
View:
{!! Form::open(['method'=>'PATCH', 'action'=>['admin\AdminUserController#registercert',$user_id], 'enctype'=>'multipart/form-data']) !!}
{!! csrf_field() !!}
...
{!! Form::close() !!}
I had a similar problem, I fixed it using a "regular" form with a POST method and adding laravel's method spoofing
<form class="form" action="/clientes/{{ $cliente->id }}" method="POST" enctype="multipart/form-data" >
{{ method_field('PUT') }}
#include('partial.cliente-campos')
</form>
The important part here is the method="POST" in the form and the {{ method_field('PUT') }}. You need both.

Laravel 5 Method not found exception

I use Laravel 5 and try to update a form:
{!! Form::model($user, ['route' => ['edit', $user->id], 'method' => 'PUT']) !!}
{!! Form::label('titel', 'First Name:'!!}
{!! Form::text('titel', null,) !!}
<button type="submit">Update</button>
{!! Form::close() !!}
My route:
Route::post('edit/{id}', ['as' => 'edit', 'uses' => 'UserController#editUser']);
My controller:
public function editUser($id){};
When click on the update Button I get MethodNotAllowedHttpException in RouteCollection.php
I checked in the browser source code and saw that Form::model(..) which I use generate the following output:
<form method="POST" action="http://localhost/myProject/public/edit/1" accept-charset="UTF-8"><input name="_method" type="hidden" value="PUT"><input name="_token" type="hidden" value="4nZlyfzzAZmTcZfThQ8gcR6cgEgYgR0ip0JZTKck">
Within the form there is the attribute method="POST" and the hidden input has the attribute value="PUT". This seems not correct for me. Any ideas? Thank you
You should use 'update' route to actually save the data (validate and persist it to the database). 'edit' route is what you used to generate edit form.
You should use PUT method to run method which saves data.
Also, here is small tip for you. Learn about how RESTful controllers work. They are really easy way to do what you're doing here (defenetly worth to learn them):
https://laravel.com/docs/5.1/controllers
Your route is not the same as your form.
Laravel uses the hidden inputs to specify the different http methods, as put.
So in your routes you should use a put method not a post.
Route::put();

Resources