If I misunderstood question then pardon.
If you need ID of the question in controller after submission, Then
Route::post('/save-answer/{question_id}, 'Controller@function_name');
``
public function function_name(Request $request, $question_id){
//validate everything in request
$question = Question::find($question_id);
$question->answers()->create($request->except('_token'));
}
``
``
<form action="{{ url('save-answer/'. $question->id) }}" class="submit_answer">
<!--token and all other inputs-->
</form>
``
``
$('.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
}
});
}
``
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community