Support the ongoing development of Laravel.io →
Forms Laravel Requests
Last updated 1 year ago.
0

If I misunderstood question then pardon.

If you need ID of the question in controller after submission, Then

  1. Get ID separately in route: i.e
     Route::post('/save-answer/{question_id}, 'Controller@function_name');
  1. In controller, receive it as second parameter

``

 public function function_name(Request $request, $question_id){

 //validate everything in request

 $question = Question::find($question_id);

 $question->answers()->create($request->except('_token'));
 }

``

  1. In the View, give the form complete url with question id, i.e

``

 <form action="{{ url('save-answer/'. $question->id) }}" class="submit_answer">
      <!--token and all other inputs-->
 </form>

``

  1. On submit in ajax just get form action for each question and send post request on route, i.e

``

 $('.submit_answer').on('submit', function(e){
 e.preventDefault();
 $.ajax({
      url: $(this).attr('action'), //get action attribute of the submitting form
      data: $(this).serialize(),
      type: 'post',
      success: function(result){
           console.log(result);
           //Do whatever you want to do here..
      },
      error: function(errors){
           console.log(errors); //These are your laravel validation errors
      }
 });
 }

``

Last updated 5 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

unixlike peronstevens Joined 30 Dec 2018

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.