app/Http/Controllers/QuestionController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        $question = Question::find($id);
        $question->question = $request->question;
        if(count($request->answers)==4){
            Answer::where('question_id',$id)->delete();
            foreach($request->answers as $answer_item){
                $answer = new Answer();
                $answer->question_id = $question->id;
                $answer->answer = $answer_item;
                $valid_1 = $answer->save();
            }
        }else{
            $error = [0,'เกิดข้อผิดพลาดบางอย่าง โปรดติดต่อแอดมิน'];
            return redirect()->back()->with(['result'=>$error[0],'text'=>$error[1]]);
        }
        $question->answer = $request->answer;
        $valid_2 = $question->save();
        if($valid_1 && $valid_2){
            $error = [1,'ลบสำเร็จ'];
        }else{
            $error = [0,'เกิดข้อผิดพลาดบางอย่าง โปรดติดต่อแอดมิน'];
        }
        return redirect()->back()->with(['result'=>$error[0],'text'=>$error[1]]);
    }
public function update ( Request $ request , $ id ) { $ question = Question :: find ( $ id ) ; $ question -> question = $ request -> question ; if ( count ( $ request -> answers ) == 4 ) { Answer :: where ( 'question_id' , $ id ) -> delete ( ) ; foreach ( $ request -> answers as $ answer_item ) { $ answer = new Answer ( ) ; $ answer -> question_id = $ question -> id ; $ answer -> answer = $ answer_item ; $ valid_1 = $ answer -> save ( ) ; } } else { $ error = [ 0 , 'เกิดข้อผิดพลาดบางอย่าง โปรดติดต่อแอดมิน']; return redirect ( ) -> back ( ) -> with ( [ 'result' => $ error [ 0 ] , 'text' => $ error [ 1 ] ] ) ; } $ question -> answer = $ request -> answer ; $ valid_2 = $ question -> save ( ) ; if ( $ valid_1 && $ valid_2 ) { $ error = [ 1 , 'ลบสำเร็จ']; } else { $ error = [ 0 , 'เกิดข้อผิดพลาดบางอย่าง โปรดติดต่อแอดมิน']; } return redirect ( ) -> back ( ) -> with ( [ 'result' => $ error [ 0 ] , 'text' => $ error [ 1 ] ] ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
//