Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ciurinho's avatar

Bug MethodNotAllowedHttpException after authentication?

Hi guys,

I have three tables - cards, notes, and users - with one-to-many relationships between cards and notes, and users and notes, respectively.

When users visit a Card page, they can fill in a form and POST a new Note. If not yet authenticated, they are redirected to the login page. For a visual cue:

My routes and controller:

After logging in I get MethodNotAllowedHttpException for 'store' and 'update' methods.

I do not get an error when creating or updating a note if the user is already signed in.

Does anyone have any idea why this is happening and how I could fix it?

0 likes
7 replies
jekinney's avatar

One idea, don't let a user access the form unless authenticated anyways.

If that doesn't work, you can utilize cache or session to store the input after logging in repopulate the form and user can resubmit. I also would suggest a note to have the user login on top of the page if auth()->guest().

wing5wong's avatar

Which method is not running? to run the Route::patch() you need to make sure to include a {{ method_field('PATCH') }} in your form

What is the Route::auth(); line?

What is line 219?

ciurinho's avatar

Hi guys, thanks for you feedback!

@wing5wong - It's 'store' and 'update', so basically anything that doesn't use GET. I have {{ method_field('PATCH') }} in my form and everything gets submitted as long as I'm logged in or I don't ask for authentication. Not sure what Route::auth(); is - I think it was added to my routes when I ran php artisan make:auth.

@Snapey, @jekinney - You are right, the easiest way to go around it would be to ask for authentication before submitting the form. The main reason why I want to keep this format is for conversion purposes. I'm going to use it for a reviews app and I think that allowing the user to read the existing reviews and post his own on the same page might work well for conversion. Ideally I would get something like this: https://en.iens.nl/restaurant/24339/amsterdam-le-restaurant (scroll to the bottom of the page to see the form).

I'm not sure how I'll get there though :) I know it's off-topic, but what does this website use? Ajax, JQuery? My Ajax and JS/JQuery skills are fairly low... Should I maybe look into Vue.js? What would be the best way to start ? Thanks!

Snapey's avatar

This is what I would do.

forget that login middleware in the controller.

Have your form as now. Have a hidden login form. As soon as the user starts typing a comment, unhide a login form between the input box and the submit button, with a message like 'hey I notice, you are not logged in, please enter your credentials..'

Then pass this whole form to the controller, the note and the credentials in one request, and at the controller do a check if they are logged in, and if not, check the form request for username and password. Attempt login and if thats sweet, commit the note to the database.

Think of it more like a login form with a note attached.

I certainly would not send them off and then back again or try ajax login... there is no real gain.

Please or to participate in this conversation.