danielbaylis/laravel-stats
Laravel package for collecting, storing, and querying application statistics in Laravel. Track events and counters, aggregate metrics over time, and retrieve reports via an easy API to monitor usage and performance in your app.
Installation
composer require danielbaylis/laravel-stats
Publish the config file (if needed):
php artisan vendor:publish --provider="DanielBaylis\LaravelStats\LaravelStatsServiceProvider"
Basic Usage Run the stats command to generate a report:
php artisan stats:generate
Outputs a JSON file (stats.json) in the project root by default.
First Use Case
php artisan stats:generate after a major refactor to verify file counts, class sizes, or dependency changes.stats.json with new team members to contextualize project structure.CI/CD Pipeline
post-merge or post-deploy steps to track growth:
# Example GitHub Actions
- name: Generate Stats
run: php artisan stats:generate
- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: project-stats
path: stats.json
Custom Reports
StatsGenerator class to filter data (e.g., exclude tests):
use DanielBaylis\LaravelStats\StatsGenerator;
class CustomStatsGenerator extends StatsGenerator {
protected function getExcludedPaths(): array {
return array_merge(parent::getExcludedPaths(), [
'tests/',
'vendor/'
]);
}
}
Dashboard Integration
stats.json in a frontend dashboard (e.g., Vue/React) to visualize:
stats.php to ignore node_modules, storage/, etc.--group-by=namespace to analyze stats per module.php artisan stats:diff to compare against a baseline (e.g., stats-baseline.json).Performance
php artisan stats:generate --cache=stats.cache
Config Overrides
config/stats.php merges correctly with defaults. Test with:
php artisan stats:generate --config=custom-path
False Positives
--exclude to filter:
php artisan stats:generate --exclude="*.blade.php"
--verbose to see excluded files:
php artisan stats:generate --verbose
php artisan stats:validate
Custom Metrics
StatsGenerator::calculateMetrics() to add bespoke stats (e.g., "average trait usage per class").Output Formats
StatsGenerator to output Markdown/CSV:
public function toMarkdown(): string {
return "## Project Stats\n- Files: " . $this->stats['total_files'];
}
Database Storage
stats.json in a DB table for historical analysis:
// Example migration
Schema::create('project_stats', function (Blueprint $table) {
$table->id();
$table->json('data');
$table->timestamps();
});
How can I help you explore Laravel packages today?