Question

How do I make my code that works locally to work on Digital Ocean

Posted March 24, 2020 199 views
ApacheDigitalOceanLaravel

I have a project on Laravel-5.8

public function store(StoreAppraisalGoalRequest $request)
{
 $appraisalStartDate = Carbon::parse($request->appraisal_start_date);
 $appraisalEndDate = Carbon::parse($request->appraisal_end_date);        
 $userCompany = Auth::user()->company_id;
 $employeeId = Auth::user()->employee_id;
  $identities = DB::table('appraisal_identity')->select('id','appraisal_name')->where('company_id', $userCompany)->where('is_current', 1)->first();
  $employees = DB::table('hr_employees')->select('id')->where('id', $employeeId)->first();
   try {
     $goal = new AppraisalGoal();
     $goal->goal_type_id             = $request->goal_type_id;
     $goal->appraisal_identity_id    = $request->appraisal_identity_id;
     $goal->employee_id              = $request->employee_id;  //$employeeId;   //$request->employees_id
     $goal->weighted_score           = $request->weighted_score;
     $goal->goal_title               = $request->goal_title;
     $goal->goal_description         = $request->goal_description;

         if ($request->appraisal_doc != "") {
             $appraisal_doc = $request->file('appraisal_doc');
             $new_name = rand() . '.' . $appraisal_doc->getClientOriginalExtension();
             $appraisal_doc->move(public_path('storage/documents/appraisal_goal'), $new_name);
            // $arr['appraisal_doc'] = $new_name;
             $goal->appraisal_doc = $new_name;
        }   
    $goal->save();        

    foreach ( $request->activity as $key => $activity){
        $startDate = Carbon::parse($request->start_date[$key]);
        $endDate = Carbon::parse($request->end_date[$key]);

        $goaldetail = new AppraisalGoalDetail();

        $goaldetail->kpi_description            = $request->kpi_description[$key];
        $goaldetail->appraisal_doc              = $request->application_doc[$key];
        $goaldetail->activity                   = $request->activity[$key];  
        $goaldetail->start_date                 = $startDate ->toDateTimeString();
        $goaldetail->end_date                   = $endDate->toDateTimeString();                  
        $goaldetail->appraisal_goal_id          = $goal->id;
        $goaldetail->appraisal_identity_id      = $goal->appraisal_identity_id;
        $goaldetail->employee_id                = $goal->employee_id;
        $goaldetail->save();
     }
    $min_date = AppraisalGoalDetail::select('start_date')->where('appraisal_goal_id', $goal->id)->min('start_date');
    $max_date = AppraisalGoalDetail::select('end_date')->where('appraisal_goal_id', $goal->id)->max('end_date');   
    $parentid = AppraisalGoalType::select('parent_id')->whereNotNull('parent_id')->where('company_id', $userCompany)->where('id', $goal->goal_type_id)->first();

     $goal->update([
         'appraisal_start_date' => $min_date,
         'appraisal_end_date'   => $max_date,
         'parent_id'            => $parentid->parent_id
             ]);


        Session::flash('success', 'Appraisal Goal is created successfully');
        return redirect()->route('appraisal.appraisal_goals.index');
  } catch (Exception $exception) {
         Session::flash('danger', 'Appraisal Goal creation failed!');
        return redirect()->route('appraisal.appraisal_goals.index');
  }
}

When I deployed to Digital Ocean, I found out that only

    $goal = new AppraisalGoal();
    $goal->goal_type_id             = $request->goal_type_id;
    $goal->appraisal_identity_id    = $request->appraisal_identity_id;
    $goal->employee_id              = $request->employee_id;  //$employeeId;   //$request->employees_id
    $goal->weighted_score           = $request->weighted_score;
    $goal->goal_title               = $request->goal_title;
    $goal->goal_description         = $request->goal_description;

         if ($request->appraisal_doc != "") {
             $appraisal_doc = $request->file('appraisal_doc');
             $new_name = rand() . '.' . $appraisal_doc->getClientOriginalExtension();
             $appraisal_doc->move(public_path('storage/documents/appraisal_goal'), $new_name);
            // $arr['appraisal_doc'] = $new_name;
             $goal->appraisal_doc = $new_name;
        }   
   $goal->save(); 

was save into the database. But,

        $goaldetail = new AppraisalGoalDetail();

        $goaldetail->kpi_description            = $request->kpi_description[$key];
        $goaldetail->appraisal_doc              = $request->application_doc[$key];
        $goaldetail->activity                   = $request->activity[$key];  
        $goaldetail->start_date                 = $startDate ->toDateTimeString();
        $goaldetail->end_date                   = $endDate->toDateTimeString();                  
        $goaldetail->appraisal_goal_id          = $goal->id;
        $goaldetail->appraisal_identity_id      = $goal->appraisal_identity_id;
        $goaldetail->employee_id                = $goal->employee_id;
        $goaldetail->save();
     }

was not saved. But on my local system everything works fine.

However, when I debugged using:

dd($exception->getMessage());

and set debug on server to true, I got this error:

“Trying to access array offset on value of type null”

How do I resolve it?

Also when I tried to restart Apache, it fails.

Thank you

edited by bobbyiliev

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi there @midowu,

I could suggest a few things:

  • Make sure that you use the same PHP version on both your local environment and on your Droplet, I can see that there is similar issue reported to the Laravel team:

https://github.com/laravel/framework/issues/30737

People are reporting that they are seeing this exact error with PHP 7.4 but it works for PHP 7.2

  • Make sure that your MySQL version is also the same

Regarding the Apache problem, make sure to run a config test to make sure that there are no configuration problems:

  • sudo apachectl -t

Hope that this helps!
Regards,
Bobby

Submit an Answer