Search code examples
laraveleloquentlaravel-8laravel-9

laravel controller form array value compere if same pass 1 not pass 0


I'm new to laravel I was trying to get value IN controller form array value compere all "ANSWER" AND "OPTION" array value.

EXAMPLE

ANSWER AND OPTION if ANSWER 0=>A EQUELS OPTION=>A pass 1 ANSWER 0=>B EQUELS OPTION=>A pass 0

THIS VALUE STORE IN ANSWER_STATUS COLUMANE

CONTROLLER

    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_STATUS; // HERE I WANT TO GET VALUE OF COMPARED 1 OR 0
     $data->question = $request->Question [$i];
     $data->answer = $request->OPTION[$i];
     $data->save();
  } 
     }

my form array

  "Question" => array:2 [▼
    0 => "YOUR NAME"
    1 => "water formula in science"
  ]
  "ANSWER" => array:2 [▼  //THIS ARRAY CONTAINING ALL RIGHT ANSWER
    0 => "A"
    1 => "h2O"
  ]
  "OPTION" => array:2 [▼  //THIS ARRAY STUDENTS ANSWER 
    0 => "A"
    1 => "CO2"
  ]
]

Solution

  • public function Store_Answer(Request $request)
    {
        $count = $request->Question;
        if ($count) {
            for ($i = 0; $i < count($request->Question); $i++) {
                if(isset($request->ANSWER[$i]) && isset($request->OPTION[$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();
                }
            }
        }
    }