vendredi 10 février 2023

laravel controller if else condition for array value

I'm Trying to pass value 1 for if equal value 0 for if not equal in columan ANSWER_STATUS using if and else condition in controller, i was tried two different methods but it was giving error help me to fix this.

ARRYR value

 "Question" => array:2 [▼
    0 => "html full form"
    1 => "water formula in science"
  ]
  "ANSWER" => array:2 [▼  //THIS ARRAY CONTAINING ALL RIGHT ANSWER
    0 => "Hypertext Markup Language"
    1 => "h2O"
  ]
  "OPTION" => array:2 [▼  //THIS ARRAY STUDENTS ANSWER 
    0 => "Markup Language"
    1 => h2O"
  ]
]

my controller method 1

public function Store_Answer(Request $request)
{
    $count = $request->Question;
    if ($count) {
        for ($i = 0; $i < count($request->Question); $i++) {
            $data = new OnlineExminAnswer();
            $data->ANSWER_STATUS = $request->ANSWER[$i] == $request->OPTION[$i] ? 1 : 0;
            $data->question = $request->Question[$i];
            $data->answer = $request->OPTION[$i];
            $data->save();
        }
    }
}

error

Trying to access array offset on value of type null

my controller method 2

 public function Store_Answer(Request $request)
    {
        $count = $request->Question;
        if ($count) {
            for ($i = 0; $i < count($request->Question); $i++) {
                $data = new OnlineExminAnswer();
                     if ($request->ANSWER[$i] == $request->OPTION[$i]) 
                     {
                     $data->ANSWER_STATUS = "1"; 
                     } else {
                     $data->ANSWER_STATUS = "0"; 
                     }  
                $data->question = $request->Question[$i];
                $data->answer = $request->OPTION[$i];
                $data->save();
            }
        }
    }

error

    Trying to access array offset on value of type null


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3lJyjY5
via IFTTT

Aucun commentaire:

Enregistrer un commentaire