Add more dynamic data error

I have a requirement that add more i will insert data as well as images to database but when i update if i remove one and update it shows error

my code is

public function actionQualification(){

    if(!empty(Yii::$app->request->get('enrolementid')) ){
      $enrolmentid=Yii::$app->request->get('enrolementid');  
    }elseif (!empty(Yii::$app->request->get('papl_id'))) {
        $enrolmentid=Yii::$app->request->get('papl_id'); 
    }else{
        $enrolmentid=Yii::$app->request->post('enrolementid');
    }

    $qualification_model = new Qualification();
    $tag='';
    if ($qualification_model->load($this->request->post())) {
     
         $userData = count($this->request->post()['Qualification']['university_name']);
       
         
        if ($userData > 1) {
            if (Yii::$app->request->get('papl_id')) {
                Qualification::deleteAll(['papl_id'=>$enrolmentid]);
            }else{
                Qualification::deleteAll(['enrolement_id'=>$enrolmentid]);
               
            }
            
            $msg ='';
            
            for ($i=0; $i < $userData; $i++) { 
                if($this->request->post()['Qualification']['university_name'][$i] !='')
                {
                        $qualification_model = new Qualification();
                        $university_name   = $this->request->post()['Qualification']['university_name'][$i];
                        $college_name  = $this->request->post()['Qualification']['college_name'][$i];
                        $year_of_passout  = $this->request->post()['Qualification']['year_of_passout'][$i];
                        $division_percent  = $this->request->post()['Qualification']['division_percent'][$i];
                        $highest_qualification  = $this->request->post()['Qualification']['highest_qualification'][$i];
                        $qualification_model->enrolement_id=$enrolmentid;
                        $qualification_model->university_name=$university_name;
                        $qualification_model->college_name=$college_name;
                        $qualification_model->year_of_passout=$year_of_passout;
                        $qualification_model->division_percent=$division_percent;
                        $qualification_model->highest_qualification=$highest_qualification;
                       
                        
                         $qualification_documents = UploadedFile::getInstances($qualification_model, 'qualification_document');
                        

                        
                                $basepath = Yii::getAlias('@storage');
                                $randnum = Yii::$app->security->generateRandomString();
                                $file = '/upload/' . $randnum .'.'. $qualification_documents[$i]->extension;
                                $path = $basepath . $file;
                                $qualification_documents[$i]->saveAs($path);
                                $qualification_model->qualification_document=$file;
                                
                                if($qualification_model->save())
                                {
                                        $msg .= "";
                                      
                                }else{
                                     $msg .= 'error '.json_encode($qualification_model->getErrors());
                                }
                           
                        
                           
                            
                }
               
            }
        }

        if($msg == ''){
            if(Yii::$app->request->post('Qualification'))
                $tag='internal';
            return $this->redirect(['enrole', 'enrolementid' => $enrolmentid,'tag'=>$tag]);
        }else{
            $tag='qualification';
            return $this->redirect(['enrole', 'enrolementid' => $enrolmentid,'tag'=>$tag,'msg' => $msg]);
        }
            
        }
        

}

where is the issue please help me???

Your issue lies here in your loop on line 409 as stated:

$qualification_documents[$i]->extension

At the start of your loop you have set the value of variable $i equal to zero (which is probably correct) - therefore on first loop iteration when the code gets to this line below - that array key has no value:

$qualification_documents[0]

Therefore you cannot call this value:

$qualification_documents[0]->extension

So to resolve this you will need to check and see what the format of this variable actually is before it gets to that line of code - and also you will need to ensure that it actually has a value assigned to it. So check this first and see what information is actually being passed - it is probably empty.

$qualification_documents

You can do something like this on the line before to see what the contents of the variable actually is before you hit that error message:

var_dump($qualification_documents);
die();

This error comes when i want to edit the documents. How to do this when i edit the records??

I can’t give you a definitive answer because I can’t see what is actually happening behind the scenes, this is something that you will need to check yourself.

But this variable assignment here:
$qualification_documents = UploadedFile::getInstances($qualification_model, 'qualification_document');

It looks as though there is no record instance getting assigned to it when you make that call. Which is why by the time you get to this line of code there is no value in your array:
$file = '/upload/' . $randnum .'.'. $qualification_documents[$i]->extension;

So this key is essentially empty:
$qualification_documents[$i]
$qualification_documents[0]

Meaning that this value is also empty:
$qualification_documents[$i]->extension
$qualification_documents[0]->extension

You first need to work out why this has no value before you can move forward. Your code is trying to assign a value to the variable $file that does not exist.

Var dump the variable $qualification_documents before you get to that line and find out what data information it actually holds before deciding how to process it.

my requirement is like this add more data and suppose i put only details without giving any image it shows error while updating that record.

when i open the image link it shows this error

Ok, so what you are saying is that when you go back to update a record it throws an error because the document may or may not be a part of the update right? Which means that sometimes you add the “Qualification Document” and sometimes you don’t yeah?

Therefore in your code block that processes the file you can do something like this:

if(!empty($qualification_documents)) {
    $basepath = Yii::getAlias('@storage');
    $randnum = Yii::$app->security->generateRandomString();
    $file = '/upload/' . $randnum .'.'. $qualification_documents[$i]->extension;
    $path = $basepath . $file;
    $qualification_documents[$i]->saveAs($path);
    $qualification_model->qualification_document=$file;
}

So in other words if this variable is not empty:
$qualification_documents

Then you process the file, or else you just ignore it altogether.

You would need to decide as to whether this works for what you want or not but something like this is an option for you. It may lead to a different issue, but equally it may not depending on how you want this to actually work.

Thanks it is working now .