jackFlick's avatar

Laravel how to pass index in multiple upload file

I've been running around the circle to fix this issue.

To explain this in summary we are trying to upload a multiple files inside an array. For each questions there's choices and each choices has image attachments.

As an example I'm trying to add 2 questions with 2 choices with image attachment each. So it will be an index of 0 and 1. It is inserting data in the database but the file that is being uploaded to index 0 is the same with index 1. So in short that data in index 0 is duplicated.

Here's my code.


foreach (array_unique($types) as $type) {
            if ($type == 'Multiple Choice') {
                
                $questions = $request->get('question_c'); // assign array of questions
                if ($request->hasFile('desc_url_c')) {
                    $image = $request->file('desc_url_c'); // assign array of images
                }
                
                // loop through the questions array
                foreach($questions as $key => $val) {

                    $choices = new AssessmentChoice();
                    $choices->setConnection($this->getDb());
                    $choices->assessment_id = $assessment->id;
                    $choices->question_c = $request->question_c[$key];
                    $choices->points_c = $request->points_c[$key];
                    $choices->description_c = $request->description_c[$key];

                    $file = $image[$key] ?? null;

                    if ($file != null) {
                        $desc_url_c = $file->store($this->getDb().'/assessments/multiple-choice');

                        $choices->desc_url_c = $desc_url_c;
                        
                    } else {
                        $choices->desc_url_c = null;
                    }

                    $choices->correct_c = $request->correct_c[$key];
                    $choices->type = 'Multiple Choice';
                    $choices->save();

                    if ($request->hasFile('option_url_c')) {
                        $images = $request->file('option_url_c'); // assign array of images
                    }
        		// decode options into array ["Option 1", "Option 2"]
                        $optionArr = json_decode($request->option_c[$key], true);
                        
        
                            foreach ($optionArr as $file => $val) {

                                $files = $images[$file] ?? null;
        
                                $choiceFiles = new AssessmentChoiceFile();
                                $choiceFiles->setConnection($this->getDb());
                                $choiceFiles->assessment_id = $assessment->id;
                                $choiceFiles->assessment_choice_id = $choices->id;
                                $choiceFiles->option_c = $val;

        
                                if ($files != null) {
                                    $choiceFile = $files->store($this->getDb().'/assessments/multiple-choice');

                                $choiceFiles->option_url_c = $choiceFile;
                                    
                                } else {
                                    $choiceFiles->option_url_c = null;
                                }
        
                                $choiceFiles->save();
                            }

                    
                }

I'm not sure if I'm doing it correctly, not sure if I'm passing the correct for the image upload.

0 likes
6 replies
jlrdw's avatar

I usually dd at key places to see what data I have, and use the network tab also to check your response.

Meaning dd and network tab while developing.

jackFlick's avatar

@jlrdw

Yes it seems that the first 2 uploads is being duplicated. This is what I got from postman.

$request->addPostParameter(array(
  'school_year_id' => '1',
  'learning_tool_id' => '',
  'lesson_id' => '1',
  'teacher_id' => '1',
  'subject_id' => '1',
  'section_id' => '1',
  'grading' => '1st Grading',
  'category' => 'Category',
  'timer' => '',
  'deadline' => '',
  'possible_score' => '100',
  'type[0]' => 'Multiple Choice',
  'question_c[0]' => 'Question 1',
  'points_c[0]' => '10',
  'description_c[0]' => 'Description',
  'option_c[0]' => '["Option 1", "Option 2"]',
  'correct_c[0]' => '["Option 1", "Option 2"]',
  'type[1]' => 'Multiple Choice',
  'question_c[1]' => 'Question 2',
  'points_c[1]' => '20',
  'description_c[1]' => 'Description 2',
  'option_c[1]' => '["Option 3", "Option 4"]',
  'correct_c[1]' => '["Option 1", "Option 2"]'
));
$request->addUpload('desc_url_c[0]', '/path/to/file', '/path/to/file', '<Content-Type Header>');
$request->addUpload('option_url_c[0]', '/C:/Users/jackvincent.balcita/OneDrive - Cardinal Health/Desktop/Pictures/YEAH.png', '/C:/Users/jackvincent.balcita/OneDrive - Cardinal Health/Desktop/Pictures/YEAH.png', '<Content-Type Header>');
$request->addUpload('option_url_c[0]', '/C:/Users/jackvincent.balcita/OneDrive - Cardinal Health/Desktop/Pictures/YEAH2.png', '/C:/Users/jackvincent.balcita/OneDrive - Cardinal Health/Desktop/Pictures/YEAH2.png', '<Content-Type Header>');
$request->addUpload('desc_url_c[1]', '/path/to/file', '/path/to/file', '<Content-Type Header>');
$request->addUpload('option_url_c[1]', '/C:/Users/jackvincent.balcita/OneDrive - Cardinal Health/Desktop/Pictures/Passport.jpg', '/C:/Users/jackvincent.balcita/OneDrive - Cardinal Health/Desktop/Pictures/Passport.jpg', '<Content-Type Header>');
$request->addUpload('option_url_c[1]', '/C:/Users/jackvincent.balcita/OneDrive - Cardinal Health/Desktop/Pictures/CSS Pharma - CRM issue.png', '/C:/Users/jackvincent.balcita/OneDrive - Cardinal Health/Desktop/Pictures/CSS Pharma - CRM issue.png', '<Content-Type Header>');

Where in the 2 uploads for option url_c[0] is being uploaded twice with the same file name.

jackFlick's avatar

@jlrdw Sorry for the late response. I'm still stuck on this :(

I've tried your advice but can't seem to figure out how I can make it work.

I just want to upload multiple files inside an array all at one submit.

[0] - 4 files [1] - 2 files

This is what I've tried

foreach ($optionArr as $file => $val) {
                        if ($request->hasFile('option_url_c')) {
                            $images = $request->file('option_url_c'); // assign array of images
                        }
                        $files = $images[$key] ?? null;
                        $choiceFiles = new AssessmentChoiceFile();
                        $choiceFiles->setConnection($this->getDb());
                        $choiceFiles->assessment_id = $assessment->id;
                        $choiceFiles->assessment_choice_id = $choices->id;
                        $choiceFiles->option_c = $val;

                        if ($files != null) {
                            $choiceFile = $files->store($this->getDb().'/assessments/multiple-choice');
                            $choiceFiles->option_url_c = $choiceFile;
                        } else {
                            $choiceFiles->option_url_c = null;
                        }
                            
                        $choiceFiles->save();

the [$key] in $files = $images[$key] ?? null; is the first foreach

and this is being uploaded, it gets duplicated and only the first file is uploaded.

[0]

  1. SCHL0000001/assessments/multiple-choice/U1n1SV8xTTzHRt4dKjGdQtPyCgag06gysJGP2iy3.jpeg
  2. SCHL0000001/assessments/multiple-choice/U1n1SV8xTTzHRt4dKjGdQtPyCgag06gysJGP2iy3.jpeg
  3. SCHL0000001/assessments/multiple-choice/U1n1SV8xTTzHRt4dKjGdQtPyCgag06gysJGP2iy3.jpeg
  4. SCHL0000001/assessments/multiple-choice/U1n1SV8xTTzHRt4dKjGdQtPyCgag06gysJGP2iy3.jpeg

[1]

  1. SCHL0000001/assessments/multiple-choice/ZbTzEgk7eNX0VUWYu8AjdY2P14c2rVYhqMxHecDU.jpeg
  2. SCHL0000001/assessments/multiple-choice/ZbTzEgk7eNX0VUWYu8AjdY2P14c2rVYhqMxHecDU.jpeg

Another option I've tried is by changing the [$key] to [$file] and this is the output. Only the first two files is uploaded and it will be duplicated on the next index.

[0]

  1. SCHL0000001/assessments/multiple-choice/v6JkP9K8LvjBMi0d0PnKAYaHJB68PJ6p7gGwfvDP.jpeg
  2. SCHL0000001/assessments/multiple-choice/dPFa5RGWjhWE375vtjgHQjcSNLLbuRyD3Rh9xxgc.jpeg
  3. NULL
  4. NULL

[1]

  1. SCHL0000001/assessments/multiple-choice/v6JkP9K8LvjBMi0d0PnKAYaHJB68PJ6p7gGwfvDP.jpeg
  2. SCHL0000001/assessments/multiple-choice/dPFa5RGWjhWE375vtjgHQjcSNLLbuRyD3Rh9xxgc.jpeg
jlrdw's avatar

You are using multipart/form-data right?

<form action='add' method='post' enctype="multipart/form-data">
jackFlick's avatar

@jlrdw

I'm currently using Postman since this is an API. I've tried option_url_c[] and option_url_c[0], option_url_c[1]

Please or to participate in this conversation.