where("id", $id)->first(); if(is_null($data)) { $data = db($tableName)->where($id)->first(); } // Safety check - ensure we have data if(is_null($data)) { Log::error("WeldLog not found for trigger execution", [ 'weld_log_id' => $id, 'table_name' => $tableName ]); return; } // Detect if this is a new record // beforeData comes from the Job - no need for additional checks $isNewRecord = false; if (is_null($beforeData)) { // Batch Excel job'dan changedColumns geliyorsa bu yeni kayıt DEĞİL, update'tir if (!empty($changedColumns)) { $isNewRecord = false; Log::info("WeldLog batch update detected (changedColumns present, beforeData null)", [ 'weld_log_id' => $id, 'reason' => 'batch_excel_update' ]); } else { $isNewRecord = true; Log::info("WeldLog new record detected", [ 'weld_log_id' => $id, 'reason' => 'beforeData_is_null' ]); } } else { // Check if all fields match - could be a new record $allFieldsMatch = true; $dataArray = (array) $data; foreach ($dataArray as $key => $value) { if (isset($beforeData->$key) && $beforeData->$key != $value) { $allFieldsMatch = false; break; } } if ($allFieldsMatch) { $isNewRecord = true; Log::info("WeldLog new record detected", [ 'weld_log_id' => $id, 'reason' => 'all_fields_match' ]); } } // Detect changed fields // Batch Excel job'dan changedColumns geliyorsa onu kullan (beforeData batch'te yanlış/null) if (!empty($changedColumns)) { $changedFields = $changedColumns; // changedColumns varsa bu bir update'tir, tüm trigger'ları çalıştırma if ($isNewRecord && !is_null($beforeData)) { $isNewRecord = false; } Log::info("WeldLog using changedColumns from batch Excel", [ 'weld_log_id' => $id, 'changed_columns_count' => count($changedColumns), 'changed_columns' => $changedColumns ]); } else { $changedFields = detectChangedFields($data, $beforeData); } Log::info("WeldLog changed fields detected", [ 'weld_log_id' => $id, 'changed_fields' => $changedFields, 'changed_fields_count' => count($changedFields), 'is_new_record' => $isNewRecord, 'source' => !empty($changedColumns) ? 'batch_excel_columns' : 'detectChangedFields', 'action' => $action ?? 'unknown' ]); // Initialize trigger system $registry = new WeldLogTriggerRegistry(); $manager = new WeldLogTriggerManager($registry); // Execute all triggers try { $results = $manager->executeTriggers( $data, $beforeData, $changedFields, $isNewRecord, $action ?? null ); Log::info("WeldLog triggers execution completed", [ 'weld_log_id' => $id, 'results_summary' => array_map(function($result) { if (isset($result['skipped']) && $result['skipped']) { return 'skipped'; } elseif (isset($result['executed']) && $result['executed']) { return 'executed'; } elseif (isset($result['queued']) && $result['queued']) { return 'queued'; } elseif (isset($result['error'])) { return 'failed'; } return 'unknown'; }, $results) ]); } catch (\Throwable $th) { Log::error("WeldLog triggers execution failed with critical error", [ 'weld_log_id' => $id, 'error' => $th->getMessage(), 'file' => $th->getFile(), 'line' => $th->getLine(), 'trace' => $th->getTraceAsString() ]); // Re-throw to ensure error is visible throw $th; }