naimul / db-visualizer
Laravel Database & Code Intelligence Visualizer (N+1, Relations, Performance Analyzer)
Requires
- php: ^8.1
- illuminate/cache: ^10.0 || ^11.0 || ^12.0 || ^13.0
- illuminate/database: ^10.0 || ^11.0 || ^12.0 || ^13.0
- illuminate/filesystem: ^10.0 || ^11.0 || ^12.0 || ^13.0
- illuminate/support: ^10.0 || ^11.0 || ^12.0 || ^13.0
Requires (Dev)
- orchestra/testbench: ^8.0 || ^9.0 || ^10.0 || ^11.0
README
A powerful Laravel package to analyze your application models, relationships, database usage, and detect performance issues like N+1 queries, unused columns, and missing eager loading.
๐ Features
- ๐ Scan all Eloquent Models (App + Modules support)
- ๐ Auto-detect relationships
- ๐ง Detect N+1 query risks probability
- โ Find unused columns
- โก Detect missing eager loading
- ๐ Performance scoring system (0โ100)
- ๐งน Code usage analysis
- ๐งฉ Supports Laravel Modules (nwidart style)
๐ฆ Installation
Install via Composer:
composer require naimul/db-visualizer
โ๏ธ Auto Service Provider
If you are using Laravel 10+, the package will auto-register.
If not, add manually:
Naimul\DbVisualizer\DbVisualizerServiceProvider::class,
๐ Publishing Configuration
To customize the package's configuration, publish the config file with:
php artisan vendor:publish --provider="Naimul\DbVisualizer\DbVisualizerServiceProvider" --tag="dbv-config"
This will create a db-visualizer.php config file in your config directory.
๐ Publishing Assets (Views, JS, CSS)
To publish the package's frontend resources (views, JS, CSS), run:
php artisan vendor:publish --provider="Naimul\DbVisualizer\DbVisualizerServiceProvider" --tag="dbv-resources"
This will copy the package's resources into your application's resources/vendor/db-visualizer directory for customization.
๐ Authorization
Access to DB Visualizer is controlled via a viewDbVisualizer gate. By default, access is only granted in the local environment.
To allow access in other environments, override the gate in your AppServiceProvider:
use Illuminate\Support\Facades\Gate; public function boot(): void { // Allow authenticated admins only Gate::define('viewDbVisualizer', fn (?User $user) => $user?->is_admin); // Allow specific emails Gate::define('viewDbVisualizer', fn (?User $user) => in_array($user?->email, [ 'admin@example.com', ])); // Allow everyone (including unauthenticated users) Gate::define('viewDbVisualizer', fn (?User $user) => true); }
Requests that fail the gate receive a 403 response. Unauthenticated users are redirected to the login page.
Important: Always declare a nullable
?User $userparameter in your gate callback. Laravel uses this to determine whether the gate supports unauthenticated (guest) access. A callback without any parameter โ such asfn () => trueโ will not be called for guests and will redirect them to login instead.
๐ Routes
After installation, visit:
/dbv
/dbv/data
/dbv/detail/{model}
Example:
http://your-app.test/dbv/data
๐ API Endpoints
Get all models analysis
GET /dbv/data
Search models
GET /dbv/data?search=User
Model details
GET /dbv/detail/User
๐ง What It Analyzes
โ Models
- Table name
- Columns
- Relations
- Soft deletes
โ Relations
- Used / unused detection
- N+1 query detection probability
- Missing eager loading
โ Columns
- Used / unused detection
โ Performance Score
-
Calculates score (0โ100)
-
Quality labels:
- Excellent
- Good
- Average
- Poor
๐ Example Response
{
"model": "User",
"table": "users",
"performance_score": 85,
"quality_label": "Good Quality",
"unused_columns_count": 2,
"n_plus_one_issues": 1,
"relations": [
{
"method": "posts",
"type": "HasMany",
"used": true,
"n_plus_one": false
}
]
}
โก Performance Scoring Rules
| Issue | Penalty |
|---|---|
| Unused relation | -10 |
| Unused column | -2 |
| N+1 issue | -15 |
| Missing eager load | -10 |
Bonus:
- Soft deletes: +5
- Cache usage: +5
- API Resource usage: +5
๐ Requirements
- PHP >= 8.1
- Laravel 10, 11, 12, 13 supported
๐ Supported Structure
app/ModelsModules/*/EntitiesModules/*/Models- Blade Views scanning
๐ฅ Use Case
This package is useful for:
- Optimizing large Laravel applications
- Detecting hidden performance issues
- Code quality auditing
- Refactoring legacy systems
- Interview/demo projects
๐งฉ Future Plans
- N+1 Query file locate
- eagerloading File locate
- Performance booster
๐ค Contributing
Pull requests are welcome. For major changes, please open an issue first.
๐ License
MIT License ยฉ Open Source
โญ Support
If you like this package, give it a โญ on GitHub.