Skip to content
Advertisement

Laravel PHP form wont submit to database or recognise my Route in web.php (but works for my first form)

I’m kind of new to Laravel. I have 2 separate pages, one is a form for teachers and one is a form for parents. The parent form works completely fine and I can see the results in my database, however, my teacher form wont submit. The page refreshes and that’s all. I noticed my Route for my teacher form wasn’t being recognised when using Route:list in the terminal. I don’t know how to get it to recognise my route for post. I’ve cleared routes and rewritten the route command but nothing is working, any suggestions would be great.

These are my Routes – originally teachers route had “post” and not “submit” – not sure that changes anything …

JavaScript

Controller functions

JavaScript

my web page where the form resides – most content in form was removed as it was too large to post

JavaScript

I’ve looked at so many sites for help and done the suggested edits but they haven’t worked so far, I’m rather clueless on how to get my teachers route to be recognised. My parents form is formatted in a very similar way and it works fine, I wasn’t sure if having 2 forms on sperate pages would be the cause, it doesn’t seem too likely. The functions for these are in the same controller.

Advertisement

Answer

if you define routes with same endpoint(like /submit) in your route files, only the last is registered because endpoint must be unique & having multiple identical endpoints is not logical & can not exist(it’s like you decide to use same address to point to different stores & homes in the city)! this is the case with your post routes. besides, you must start your endpoints with /

you defined two identical post routes to /submit which laravel registers the last one which points to submitparentform() method & that is why you don’t see first post route which points to submitteacherform()

define your endpoints like /submit/teacherform & /submit/parentform

duplicate Auth::routes(); is also unnecessary

some extra tips for cleaner code 😉

  1. if all of your validation messages are equal (third parameter of $this->validate()), instead of repeating it many times, just write: "*.required"=>"All questions must be filled in", * means everything
  2. when name of inputs match corresponding table fields in database, instead of sql query use eloquent & simply ChecklistParent::create($request->all()); & of course $request->all() may need additional data or some exclusion, in your case it becomes:

$request->merge(['user_id' => 1])->except(['_token'])

eloquent makes working with database easy, read more about eloquent here

  1. if all of your fields are required, instead of repeating name of all fields, you can use this simple trick by @okiemute-omuta for validation.

& use die() & echo() only for testing purposes

hope it was resolving, best regards 😉

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement