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

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>

Related

Methods (PUT, UPDATE, DELETE) that are not supported in some routes in 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

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') }}">

Getting error like "The GET method is not supported for this route. Supported methods: POST."

I want to edit the post so gone through the edit route process but it's showing error like
The GET method is not supported for this route. Supported methods: POST.
I tried in edit.blade.php method="post" and also "POST" and in route.php changed Route::POST to Route::Post/Put
In edit.blade.php
<form class="form"action="{{route('Staff.update')}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
and in web.php
Route::get('/Staff/edit/{id}',[
'uses'=>'StaffController#edit',
'as'=>'staff.edit'
]);
Route::Post('/Staff/update/{id}',[
'uses'=>'Staffcontroller#update',
'as'=>'Staff.update'
]);

Logging out via a link in Laravel

I have a "Logout" link in my top navigation bar. I'm wondering how I can make it so that while I'm logged in, it'll log me out when I click on it and return me to the homepage.
To be specific, what changes to which files do I make in Laravel? Also, what code do I need to write in the view, which currently contains just HTML, to trigger this?
When you run php artisan make:auth, the default app.php in Laravel 5.5 does it like this:
<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
Edited 28/12/2019: It's work, but This answer contains a serious security issue. Please consider before using it. The Answer by Lucas Bustamante maybe a better choice. Refer to the comment section of this answer.
1) if you are using the auth scaffold that laravel contains. You can do this, in your navigation bar add this:
logout
then add this to your web.php file
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController#logout');
Done. This will logout you out and redirect to homepage. To get the auth scaffold, from command line, cd into your project root directory and run
php artisan make:auth
2) add this to your navigation bar:
logout
then add this in your web.php file
Route::get('/logout', 'YourController#logout');
then in the YourController.php file, add this
public function logout () {
//logout user
auth()->logout();
// redirect to homepage
return redirect('/');
}
Done.
Read:
https://mattstauffer.co/blog/the-auth-scaffold-in-laravel-5-2
https://www.cloudways.com/blog/laravel-login-authentication/
Use the logout() method:
auth()->logout();
Or:
Auth::logout();
To log users out of your application, you may use the logout method on the Auth facade. This will clear the authentication information in the user's session.
if you want to use jQuery instead of JavaScript:
<a href="javascript:void" onclick="$('#logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>
As the accepted answer mentions that logging out via GET has side effects you should use the default POST route already created by Laravel auth.
Simply create a little form and submit it via link or button HTML tag:
<form action="{{ route('logout') }}" method="POST">
#csrf
<button type="submit">
{{ __('Logout') }}
</button>
</form>
If you use guard you can logout using this line of code :
Auth::guard('you-guard')->logout();
in laravel 8.x
#csrf
<x-jet-dropdown-link href="{{ route('logout') }}"
onclick="event.preventDefault();
this.closest('form').submit();">
{{ __('Logout') }}
</x-jet-dropdown-link>
</form>

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

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.

Resources