1414
1515namespace RodrigoPedra \QueryLogger ;
1616
17+ use Illuminate \Contracts \Config \Repository ;
1718use Illuminate \Database \Events \QueryExecuted ;
1819use Psr \Log \LoggerInterface ;
1920
2021class QueryLogger
2122{
2223 protected LoggerInterface $ logger ;
24+ protected Repository $ config ;
2325
24- public function __construct (LoggerInterface $ logger )
26+ public function __construct (LoggerInterface $ logger, Repository $ config )
2527 {
2628 $ this ->logger = $ logger ;
29+ $ this ->config = $ config ;
2730 }
2831
2932 public function handle (QueryExecuted $ event )
@@ -41,6 +44,7 @@ public function handle(QueryExecuted $event)
4144 'bindings ' => $ event ->bindings ,
4245 'time ' => $ event ->time ,
4346 'connection ' => $ event ->connectionName ,
47+ 'database ' => $ this ->config ->get ("database.connections. {$ event ->connectionName }.database " ),
4448 ]);
4549 }
4650
@@ -57,7 +61,7 @@ protected function prepareQuery(string $query, array $bindings): string
5761 return $ query ;
5862 }
5963
60- protected function prepareValue ($ pdo , $ value ): string
64+ protected function prepareValue (? \ PDO $ pdo , $ value ): string
6165 {
6266 if (\is_null ($ value )) {
6367 return 'NULL ' ;
@@ -75,23 +79,23 @@ protected function prepareValue($pdo, $value): string
7579 return $ this ->quote ($ pdo , '[BINARY DATA] ' );
7680 }
7781
78- if (\is_object ($ value ) && \method_exists ($ value , '__toString ' )) {
79- $ value = \strval ($ value );
80- }
81-
8282 if (\is_object ($ value ) && \method_exists ($ value , 'toString ' )) {
8383 $ value = $ value ->toString ();
8484 }
8585
86- if (\is_object ( $ value) && \is_a ( $ value , \ DateTimeInterface::class) ) {
86+ if ($ value instanceof \ DateTimeInterface) {
8787 $ value = $ value ->format ('Y-m-d H:i:s ' );
8888 }
8989
90+ if ($ value instanceof \Stringable) {
91+ $ value = \strval ($ value );
92+ }
93+
9094 // objects not implementing __toString() or toString() will fail here
9195 return $ this ->quote ($ pdo , \strval ($ value ));
9296 }
9397
94- protected function quote ($ pdo , string $ value ): string
98+ protected function quote (? \ PDO $ pdo , string $ value ): string
9599 {
96100 if ($ pdo ) {
97101 return $ pdo ->quote ($ value );
0 commit comments