diff --git a/src/Illuminate/Database/Connection.php b/src/Illuminate/Database/Connection.php index eb14815b87b9..d3ea5cd65235 100755 --- a/src/Illuminate/Database/Connection.php +++ b/src/Illuminate/Database/Connection.php @@ -763,7 +763,7 @@ protected function run($query, $bindings, Closure $callback) // then log the query, bindings, and execution time so we will report them on // the event that the developer needs them. We'll log time in milliseconds. $this->logQuery( - $query, $bindings, $this->getElapsedTime($start) + $query, $bindings, $this->getElapsedTime($start), $this->getRowCount($result) ); return $result; @@ -821,16 +821,17 @@ protected function isUniqueConstraintError(Exception $exception) * @param string $query * @param array $bindings * @param float|null $time + * @param int|null $count * @return void */ - public function logQuery($query, $bindings, $time = null) + public function logQuery($query, $bindings, $time = null, $count = null) { $this->totalQueryDuration += $time ?? 0.0; - $this->event(new QueryExecuted($query, $bindings, $time, $this)); + $this->event(new QueryExecuted($query, $bindings, $time, $this, $count)); if ($this->loggingQueries) { - $this->queryLog[] = compact('query', 'bindings', 'time'); + $this->queryLog[] = compact('query', 'bindings', 'time', 'count'); } } @@ -845,6 +846,17 @@ protected function getElapsedTime($start) return round((microtime(true) - $start) * 1000, 2); } + /** + * Get the affected row count. + * + * @param mixed $result + * @return int|null + */ + protected function getRowCount($result) + { + return is_array($result) ? count($result) : (is_int($result) ? $result : null); + } + /** * Register a callback to be invoked when the connection queries for longer than a given amount of time. * diff --git a/src/Illuminate/Database/Events/QueryExecuted.php b/src/Illuminate/Database/Events/QueryExecuted.php index 833a21e6f984..054055d1981a 100644 --- a/src/Illuminate/Database/Events/QueryExecuted.php +++ b/src/Illuminate/Database/Events/QueryExecuted.php @@ -39,6 +39,13 @@ class QueryExecuted */ public $connectionName; + /** + * The affected row count. + * + * @var int + */ + public $count; + /** * Create a new event instance. * @@ -46,14 +53,16 @@ class QueryExecuted * @param array $bindings * @param float|null $time * @param \Illuminate\Database\Connection $connection + * @param int|null $count * @return void */ - public function __construct($sql, $bindings, $time, $connection) + public function __construct($sql, $bindings, $time, $connection, $count) { $this->sql = $sql; $this->time = $time; $this->bindings = $bindings; $this->connection = $connection; $this->connectionName = $connection->getName(); + $this->count = $count; } }