Processing multi-step form in Laravel

Viewed 1093

I am quite new to laravel I have to insert many form fields in database so I divided the fields into multiple sections what I want to achieve is to store data of each section when user clicks next button and step changes and when user clicks previous button and makes some changes the database should be updated and if user leaves the form incomplete then when he logins next time form fill should fill up from the step he left in, till now i have successfully achieved to change steps and in first step 1 inserted the data into database and for other step i updated the database but I am having trouble if user comes to first step and again changes the forms fields how to update again first step data i am using ajax to send data and steps number

My Controller

    function saveJobPostFirstStage(Request $request)
    {
    $currentEmployer = Auth::guard('employer')->user();
   //$data['currentEmployer'] = $currentEmployer;
     $employer_id = $currentEmployer->id;
      $random = $this->generateRandomString();
       $jobOne = new Job();
       //Session::pull('insertedId');

  if ($request->ajax()) {

        try {

            $stepPost = $request->all();

                $step = $stepPost['stepNo'];
                $insertedId = $stepPost['insertedId'];
                switch ($step) {
                    case '1':
                        if ($insertedId == 0) {

                        $jobOne->employer_id = $employer_id;
                        $jobOne->job_title = $stepPost['jobTitle'];

                        $jobOne->company_id = (int)$stepPost['companyName'];
                        $jobOne->country_id = (int)$stepPost['country'];
                        $jobOne->state_id = (int)$stepPost['state'];
                        $jobOne->city_id = (int)$stepPost['city'];
                        $jobOne->street_address = $stepPost['street'];
                        $jobOne->job_code = $random;


                        $stepOne = $jobOne->save();
                          if ($stepOne) {

                               Session::put('insertedId',$jobOne->id);
                           //session(['insertedId'=>$jobOne->id]);
                                $success = ['success' => "Success",
                                                'insertedId' => $jobOne->id];
                                //return json_encode($success);  

                              } 

                        }
                        else
                        {
                        $jobOne->employer_id = $employer_id;
                        $jobOne->job_title = $stepPost['jobTitle'];

                        $jobOne->company_id = (int)$stepPost['companyName'];
                        $jobOne->country_id = (int)$stepPost['country'];
                        $jobOne->state_id = (int)$stepPost['state'];
                        $jobOne->city_id = (int)$stepPost['city'];
                        $jobOne->street_address = $stepPost['street'];
                        $jobOne->job_code = $random;

                        $stepOne = $jobOne->whereId($insertedId)->update(['employer_id'=>$jobOne->employer_id,'job_title'=>$jobOne->job_title,'company_id'=> $jobOne->company_id,'state_id'=>$jobOne->state_id,'country_id'=>$jobOne->country_id,'city_id'=>$jobOne->city_id,'street_address'=>$jobOne->street_address,'job_code'=>$jobOne->job_code = $random]);
                        if ($stepOne) {

                                $success = ['success' => "Changes Made Successfully"];
                                return json_encode($success);        
                              } 
                        }
                        break;

                        case '2':


                            $jobOne->employment_type_id = (int)($stepPost['employmentType']);
                            $jobOne->job_type_id = (int)($stepPost['jobType']);
                            $jobOne->job_level_id = (int)($stepPost['jobLevel']);
                            $jobOne->industry_type_id = (int)($stepPost['industryType']);
                            $jobOne->job_category_id = (int)($stepPost['jobCategory']);
                            //$jobOne->salary = $stepPost['jobSalaryRange'];
                            $jobOne->salary_period_id = (int)$stepPost['salaryPeriod'];
                            //$jobOne->vacancy_end_date = $stepOne['applicationDeadline'];
                            $stepOne = $jobOne->whereId($insertedId)->update(['employment_type_id'=> $jobOne->employment_type_id,'job_type_id'=>$jobOne->job_type_id,'job_level_id'=> $jobOne->job_level_id,'industry_type_id'=>$jobOne->industry_type_id,'job_category_id'=>$jobOne->job_category_id,'salary_period_id'=>$jobOne->salary_period_id]);
                        if ($stepOne) {

                                $success = ['success' => "Changes Made Successfully"];
                                return json_encode($success);        
                              } 



                        break;

                        case '3':
                        $jobOne->job_description = $stepPost['jobDescription'];
                        $jobOne->job_specification = $stepPost['jobSpecifications'];
                        $jobOne->job_responsibilities = $stepPost['jobResponsibilities']; 

                        $stepOne = $jobOne->whereId($insertedId)->update(['job_description'=>$jobOne->job_description,'job_specification'=>$jobOne->job_specification,'job_responsibilities'=>$jobOne->job_responsibilities]);

                        if ($stepOne) {
                             $success = ['success' => "Changes Made Successfully"];
                                return json_encode($success);  
                        }

                    default:
                        # code...
                        break;
                }


            return json_encode($stepPost);







        //$this->alertMessage = 'Your Phone has been added Successfully.';
        //$this->alertType = 'success';
    } catch (QueryException $e) {
        return $e->getMessage();
    }

   /* return redirect()->route('employer-account-page')
        ->with([
            'alertMessage' => $this->alertMessage,
            'alertType' => $this->alertType
        ]);*/

      // $stepPost = Input::all();



  }
        /*$stepOne = $request->all();
        $country_Id = (int)$stepOne['country'];
        return json_encode((getType($country_Id)));*/



}
1 Answers

First of, your code is messy.

You should have a single table per form where each form have it's parent id. The next step to refactor the code would be to create a single controler per form (you don't need it, but you want this)

Each form (a model) should have a method that recalculates the values of itself based on other forms, so that if you change a first form, then you can call the method that recalculates the second form, then call method of second form that recalculates the third form, etc.

This interface could be helpful

interface IForm {
    public function getPreviousForm() : ?IForm; // These notations are since PHP7.1
    public function recalculate() : void;
    public function getNextForm() : ?IForm;
}

A simple code how it should work in practice

$formX->save();
$formX->getNextForm()->recalculate(); // This will call formX->recalculate(); formX+1->getNextForm()->recalculate()
                                    // which will call formX+1->recalculate(); formX+2->getNextForm()->recalculate()
                                    // etc...
                                    // while getNextForm() != null

You may also need this if you would need to insert another form in the middle of the chain.

Hope it helps

Related