public function toChangesArray(TimesheetEvent $other) { $attributes_old = parent::toArray(); $attributes_new = $other->toArray(); $skip_keys = ['id' => 1, 'created_at' => 1, 'updated_at' => 1, 'deleted_at' => 1, 'org_data' => 1, 'update_data' => 1]; $zeroisempty_keys = ['discount' => 1]; $result = []; // Find all the values that where changed or deleted foreach ($attributes_old as $key => $value) { // Skip null values, keys we don't care about and 0 value keys that means they are not used if (empty($value) || isset($skip_keys[$key]) || isset($zeroisempty_keys[$key]) && $value) { continue; } // Compare values if it exists in the new array if (isset($attributes_new[$key]) || array_key_exists($key, $attributes_new)) { if ($value instanceof \DateTime && $attributes_new[$key] instanceof \DateTime) { if ($value != $attributes_new[$key]) { $result[$key] = $attributes_new[$key]->format("Y-m-d H:i:s"); } } elseif ($value instanceof \DateTime && is_string($attributes_new[$key])) { if ($value->format("Y-m-d H:i:s") != $attributes_new[$key]) { $result[$key] = $attributes_new[$key]; } } elseif (is_string($value) && $attributes_new[$key] instanceof \DateTime) { if ($attributes_new[$key]->format("Y-m-d H:i:s") != $value) { $result[$key] = $attributes_new[$key]->format("Y-m-d H:i:s"); } } elseif ($value != $attributes_new[$key]) { $result[$key] = $attributes_new[$key]; } } else { $result[$key] = null; } } // Find all the values that where deleted foreach ($attributes_new as $key => $value) { if (isset($skip_keys[$key])) { continue; } if (!isset($attributes_old[$key])) { $result[$key] = $value; } } return $result; }