you say:
...The Ajax request has two post variables: question_id, answer....
you mean question_id? because in the store() method, you use question:
'question_id' => request('question'),
might this be the cause?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Good morning! I have an error in my application which I just can't manage to fix.
The application is basically a multiple choice quiz. All questions (around 60 per quiz) are pre-loaded on the page and every answer is submitted with an Ajax call.
The Ajax request has two post variables: question_id, answer. The Ajax creates a POST request to a Controller, which in turn calls a Repository that should save the given answer in the Database.
However, if I check my logs I see that the Request value sometimes is null and I really don't know how this is possible.
Here is part of the code:
An Ajax request with CSRF header and POST data is submitted to the saveAnswer() method below:
class AnswerController extends Controller
{
/**
* @var UserAnswerRepository
*/
protected $user_answer;
/**
* AnswerController constructor.
* @param UserAnswerRepository $user_answer
*/
public function __construct(UserAnswerRepository $user_answer)
{
$this->user_answer = $user_answer;
}
/**
*
*/
public function saveAnswer()
{
$this->user_answer->store();
}
}
Then in the UserAnswerRepository the store method is called to save the given answer to the database:
public function store()
{
UserAnswer::create([
'question_id' => request('question'), // question = question_id
'test_session_id' => session()->get('test_session'),
'answer' => request('answer')
]);
}
The error was thrown because question_id and answer are null.
Can someone help me?
Please or to participate in this conversation.