@@ -65,17 +65,44 @@ private function executeWithTiming(string $methodName, array $arguments)
6565 $ sql = preg_replace ('/\?/ ' , $ value , $ sql , 1 );
6666 }
6767
68+ // Capture the call stack up to the maximum depth
69+ $ maxStackDepth = $ builderConfig ['maxStackDepth ' ] ?? 5 ;
70+ $ stackTrace = $ this ->getStackTrace ($ maxStackDepth );
71+
6872 // Log the slow Eloquent method execution
6973 Log::info ('QueryMonitor: Slow Eloquent method detected ' , [
7074 'method ' => $ methodName ,
71- 'execution_time ' => $ executionTime . ' ms ' ,
75+ 'arguments ' => $ arguments ,
7276 'query ' => $ sql ,
77+ 'execution_time ' => $ executionTime . ' ms ' ,
78+ 'stack_trace ' => $ stackTrace ,
7379 ]);
7480
7581 // Return the result of the parent method
7682 return $ results ;
7783 }
7884
85+ /**
86+ * Captures the stack trace up to the specified depth.
87+ */
88+ private function getStackTrace (int $ maxStackDepth ): array
89+ {
90+ $ backtrace = debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS , $ maxStackDepth + 1 );
91+ $ stackTrace = [];
92+
93+ for ($ i = 1 ; $ i <= $ maxStackDepth && isset ($ backtrace [$ i ]); $ i ++) {
94+ $ frame = $ backtrace [$ i ];
95+ $ stackTrace [] = [
96+ 'file ' => $ frame ['file ' ] ?? '' ,
97+ 'line ' => $ frame ['line ' ] ?? '' ,
98+ 'class ' => $ frame ['class ' ] ?? '' ,
99+ 'function ' => $ frame ['function ' ] ?? '' ,
100+ ];
101+ }
102+
103+ return $ stackTrace ;
104+ }
105+
79106 /**
80107 * Overrides the get method to include performance monitoring.
81108 *
@@ -126,10 +153,10 @@ public function pluck($column, $key = null)
126153 /**
127154 * Overrides the paginate method to include performance monitoring.
128155 *
129- * @param int|null $perPage Items per page.
130- * @param array|string $columns The columns to retrieve.
131- * @param string $pageName The page query string parameter.
132- * @param int|null $page The current page.
156+ * @param int|null $perPage Items per page.
157+ * @param array|string $columns The columns to retrieve.
158+ * @param string $pageName The page query string parameter.
159+ * @param int|null $page The current page.
133160 * @return LengthAwarePaginator The paginator instance.
134161 */
135162 public function paginate ($ perPage = null , $ columns = ['* ' ], $ pageName = 'page ' , $ page = null )
0 commit comments