Home > Software engineering >  iam have code php and get this error Trying to access array offset on value of type null
iam have code php and get this error Trying to access array offset on value of type null

Time:02-17

This is my code:

 $evaluationjob = evaluation_elements_jobs::where('job_id', $user->job_id)
          ->where('company_id',$company_check->id)
          ->first();

The error in this line:

         $items = json_decode($evaluationjob["element_degree"]);

The error message is:

Trying to access array offset on value of type null

CodePudding user response:

You can try this:

$evaluationjob = evaluation_elements_jobs::where('job_id', $user->job_id)
      ->where('company_id',$company_check->id)
      ->first();

if ($evaluationjob != null && isset($evaluationjob["element_degree"])) {
    $items = json_decode($evaluationjob["element_degree"]);
}

This will check if $evaluationjob is not null and if the value $evaluationjob["element_degree"] exists in the variable.

CodePudding user response:

The error you are having here is the fact that you're trying to access a property on a variable that is null;

So you need to put a check on the variable and the property.

if ($evaluationjob != null && isset($evaluationjob["element_degree"])) {
    $items = json_decode($evaluationjob["element_degree"]);
}
  • Related