where("id", $id)->first(); if(is_null($lineList)) { $lineList = db($tableName)->where($id)->first(); } // Safety check - ensure we have data if(is_null($lineList)) { Log::error("LineList not found for trigger execution", [ 'line_list_id' => $id, 'table_name' => $tableName ]); return; } // Detect changes and new record status $isNewRecord = is_null($beforeData); $changedFields = detectChangedFields($lineList, $beforeData); // Execute triggers via manager $registry = new LineListTriggerRegistry(); $manager = new LineListTriggerManager($registry); try { $results = $manager->executeTriggers( $lineList, $beforeData, $changedFields, $isNewRecord ); Log::info("LineList triggers execution completed", [ 'line_list_id' => $id, 'line_no' => $lineList->line_no ?? 'NULL', 'results_summary' => array_map(function($result) { if (isset($result['skipped']) && $result['skipped']) { return 'skipped'; } elseif (isset($result['success']) && $result['success']) { return 'success'; } elseif (isset($result['error'])) { return 'failed'; } return 'unknown'; }, $results) ]); } catch (\Throwable $th) { Log::error("LineList triggers execution failed with critical error", [ 'line_list_id' => $id, 'line_no' => $lineList->line_no ?? 'NULL', 'error' => $th->getMessage(), 'file' => $th->getFile(), 'line' => $th->getLine(), 'trace' => $th->getTraceAsString() ]); throw $th; }