app/Http/Controllers/QuestionController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Question  $question
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Question $question)
    {
        try{
            $validator = Validator::make(request()->all(),[
                'question'  => "required",
                'survey_id' => 'required',
                'step_no'   => 'required|integer',
            ]);
            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            }
            $data = request()->all();
            if(array_key_exists('is_required',$data)){
                $data['is_required']=1;
            }
            if(array_key_exists('json_option',$data)){
                if(count($data['json_option'])>0){
                    foreach($data['json_option'] as $d){
                        if(is_null($d))
                            throw new Exception("Options can't be Null Or Empty!", 403);      
                    }
                }
                $data['json_option']= json_encode($data['json_option']);
            }
            if(array_key_exists('radio',$data)){
                unset($data['radio']);
            }
            if(array_key_exists('checkbox',$data)){
                unset($data['checkbox']);
            }
            if($data['type']=='dropdown'){
                if(!array_key_exists('json_option',$data)) throw new Exception("Options can't be Null Or Empty!", 403); 
            }
            $data['updated_by'] = auth()->user()->id??null;
            $question->update($data);
            return back()->withSuccess('Question Updated Successfully!');
        } catch (Exception $e) {
            return redirect()->back()->withDanger($e->getMessage());
        }
    }
public function update ( Question $ question ) { try { $ validator = Validator :: make ( request ( ) -> all ( ) , [ 'question' => " required " , 'survey_id' => 'required' , 'step_no' => 'required|integer' , ] ) ; if ( $ validator -> fails ( ) ) { return redirect ( ) -> back ( ) -> withErrors ( $ validator ) -> withInput ( ) ; } $ data = request ( ) -> all ( ) ; if ( array_key_exists ( 'is_required' , $ data ) ) { $ data [ 'is_required' ] = 1 ; } if ( array_key_exists ( 'json_option' , $ data ) ) { if ( count ( $ data [ 'json_option' ] ) > 0 ) { foreach ( $ data [ 'json_option' ] as $ d ) { if ( is_null ( $ d ) ) throw new Exception ( " Options can't be Null Or Empty! " , 403 ) ; } } $ data [ 'json_option' ] = json_encode ( $ data [ 'json_option' ] ) ; } if ( array_key_exists ( 'radio' , $ data ) ) { unset ( $ data [ 'radio' ] ) ; } if ( array_key_exists ( 'checkbox' , $ data ) ) { unset ( $ data [ 'checkbox' ] ) ; } if ( $ data [ 'type' ] == 'dropdown' ) { if ( ! array_key_exists ( 'json_option' , $ data ) ) throw new Exception ( " Options can't be Null Or Empty! " , 403 ) ; } $ data [ 'updated_by' ] = auth ( ) -> user ( ) -> id ?? null ; $ question -> update ( $ data ) ; return back ( ) -> withSuccess ( 'Question Updated Successfully!' ) ; } catch ( Exception $ e ) { return redirect ( ) -> back ( ) -> withDanger ( $ e -> getMessage ( ) ) ; } }
Update the specified resource in storage.
Update the specified resource in storage .