adamjhn's avatar

How to store the file uploaded for a dynamic question of type file?

I have a form that can have dynamic questions of any type (text, checkbox, file, radio button, textarea). To store the answers to these questions there is this code:

    if (isset($request['answer'][$ticket][$nameKey])) {
        foreach ($request['answer'][$ticket][$nameKey] as $question_id => $answer) {
            $answer = Answer::create([
                'question_id' => $question_id,
                'user_id' => $user->id,
                'answer' => $answer,
            ]);
        }
    }

But like this if the dynamic question is of type file, is only storing in the answers table like:

    id      user_id        question_id         answer
    1           5                    1      pdftest.pdf

But the file is not being stored in any folder, is only stored the name in db. Do you know what is necessary to detect if its a question of type file? And if it is store the file in some folder so then is possible to access the file?

0 likes
6 replies
adamjhn's avatar

Thanks, but for example the $request->all() shows:

"answer" => array:1 [▼
    18 => array:1 [▼
      1 => array:1 [▼
        4 => "pdftest.pdf"
      ]
    ]
  ]

The pdftest.pdf is the uploaded file for the dynamic question. What should be passed in the hasFile() method to detect that the answer array has 1 or more files as answers?

Cronix's avatar

Maybe $request['answer'][$ticket][$nameKey]? is $request the actual request object? Can you tell what kind of type it is by the question_id?

1 like
adamjhn's avatar

Yes, the questions table has the column "type" and " $request['answer'][$ticket][$nameKey]" is the question_id.

Cronix's avatar

what does this show?

foreach ($request['answer'][$ticket][$nameKey] as $question_id => $answer) {
    if ($request->hasFile($answer)) {
        echo "$question_id has a file<br>";
    }
}

I don't want to help sort out your overly complex array stuff, but I did answer your question of "Do you know what is necessary to detect if its a question of type file?". As far as saving it, it's just like the docs show once you know it's a file. You just need to figure out how to access the field in $request.

1 like
adamjhn's avatar

It shows that the question 4 has a file. But also shows that question 5 has a file but the question id 5 has the column type as "text".

Please or to participate in this conversation.