resources/views/update/controller/QuestionController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $rules = array(
            'question' => 'required',
            'question_type' => 'required',
        );
        $data = [
            'question' => trim($request->get('question')),
            'question_type' => trim($request->get('question_type')),
            ];
        $validator = Validator::make($data,$rules);
    if($validator->fails())
       {
        return  response()->json(['errors'=>$validator->errors()]);
       }else
       {
        $user_id = Auth::user()->id;
            if(isset($request->edit_id) && ($request->edit_id !="") )
            {
            $data = Question::findOrFail($request->edit_id);
            $data->question = $request->question;
            $data->category_id = $request->category_id;
            $data->user_id = $user_id;
            $data->question_type = $request->question_type;
            $data->status     = $request->status;
            $data->save(); 
            $success = 'Question has been updated.';
            return response()->json($success);
            }else{
            $data = New Question;
            $data->question = $request->question;
            $data->category_id = $request->category_id;
            $data->user_id = $user_id;
            $data->question_type = $request->question_type;
            $data->status     = $request->status;
            $data->editedable     = '1';
            $data->save();
            if($data){
               foreach ($request->options as $key => $value) {
                    $options = $request['options'][$key];
                    $option = new QuestionOption;
                    $option->question_id = $data['id'];
                    $option->options = $options;
                    $option->status = 'Active';
                    $option->save();
               }
            } 
            $success = 'Question has been created.';
            return response()->json($success);
           }
        }
    }
public function store ( Request $ request ) { $ rules = array ( 'question' => 'required' , 'question_type' => 'required' , ) ; $ data = [ 'question' => trim ( $ request -> get ( 'question' ) ) , 'question_type' => trim ( $ request -> get ( 'question_type' ) ) , ] ; $ validator = Validator :: make ( $ data , $ rules ) ; if ( $ validator -> fails ( ) ) { return response ( ) -> json ( [ 'errors' => $ validator -> errors ( ) ] ) ; } else { $ user_id = Auth :: user ( ) -> id ; if ( isset ( $ request -> edit_id ) && ( $ request -> edit_id != " " ) ) { $ data = Question :: findOrFail ( $ request -> edit_id ) ; $ data -> question = $ request -> question ; $ data -> category_id = $ request -> category_id ; $ data -> user_id = $ user_id ; $ data -> question_type = $ request -> question_type ; $ data -> status = $ request -> status ; $ data -> save ( ) ; $ success = 'Question has been updated.' ; return response ( ) -> json ( $ success ) ; } else { $ data = New Question ; $ data -> question = $ request -> question ; $ data -> category_id = $ request -> category_id ; $ data -> user_id = $ user_id ; $ data -> question_type = $ request -> question_type ; $ data -> status = $ request -> status ; $ data -> editedable = '1' ; $ data -> save ( ) ; if ( $ data ) { foreach ( $ request -> options as $ key => $ value ) { $ options = $ request [ 'options' ] [ $ key ] ; $ option = new QuestionOption ; $ option -> question_id = $ data [ 'id' ] ; $ option -> options = $ options ; $ option -> status = 'Active' ; $ option -> save ( ) ; } } $ success = 'Question has been created.' ; return response ( ) -> json ( $ success ) ; } } }
Store a newly created resource in storage.
Store a newly created resource in storage .
//$data->question_order = $request->question_order; //$data->question_order = $request->question_order; // $option->user_id = $user_id;