clcbws / laravel-eloquent-insight
Premium Eloquent performance suite with N+1 resolution, ghost relation detection, hydration profiling, and AST auto-fixing.
Package info
github.com/ahtesham-clcbws/laravel-eloquent-insight
pkg:composer/clcbws/laravel-eloquent-insight
Requires
- php: ^8.2
- illuminate/console: ^13.0|^12.0|^11.0
- illuminate/database: ^13.0|^12.0|^11.0
- illuminate/support: ^13.0|^12.0|^11.0
- nikic/php-parser: ^5.0
Requires (Dev)
- orchestra/testbench: ^9.0|^10.0
- phpunit/phpunit: ^10.0|^11.0
README
Laravel Eloquent Insight is a premium performance suite that identifies, ranks, and resolves database bottlenecks. While most tools just "shout" at you when an N+1 violation happens, Insight provides the intelligence to understand why and the tools to fix it automatically.
Fully optimized for Laravel 13.x, with legacy support for 12.x and 11.x.
โ Why Eloquent Insight?
In standard Laravel development, the N+1 Problem is the #1 killer of application performance. You load 50 users, and Laravel accidentally fires 51 queries to fetch their profiles.
The Problem with Other Tools:
- ๐ข Runtime Overhead: They slow down your production app while "watching" for errors.
- ๐ฃ Noise: They give you a flat list of errors without telling you which ones actually hurt your performance.
- ๐ง Manual Labor: You still have to manually find the code and fix it.
The Insight Solution:
- โก Zero Production Overhead: Recording only happens in dev; production uses a static O(1) manifest.
- ๐ Impact Scoring: We tell you which bottlenecks are CRITICAL (e.g., 100+ redundant queries) vs. moderate.
- ๐ง Auto-Refactoring: Our AST-engine can actually write the fix into your code for you.
โจ Key Features (The Elite Suite)
๐ Impact Scoring
Not all N+1s are equal. Insight ranks every violation:
- ๐ด CRITICAL: Generating 50+ redundant queries. Fix immediately!
- ๐ HIGH: Generating 10-50 queries. Significant slowdown.
- ๐ต MODERATE: Low-level redundancy.
๐ป Ghost Relation Detection
Sometimes you use ->with(['user', 'profile']) but then delete the code that used the profile. The profile is still being loaded, wasting memory and DB time. Insight flags these "Ghost Relations" so you can strip them out.
๐ง Hydration Profiling (The "Fat Model" Finder)
If you SELECT * on a table with 50 columns but only display the name, you are wasting memory. Insight compares what you fetched vs. what you accessed, suggesting surgical ->select([...]) arrays.
๐บ๏ธ Query Topology Visualization
Complex apps have messy relationship chains. Insight generates a Mermaid.js graph that shows exactly how your queries flow, helping you visualize the "execution distance" between your models.
๐ Redundancy (Duplicate) Detection
Detects identical SQL queries with matching bindings fired in the same requestโoften a sign of redundant service calls or missing caching logic.
๐ The "Capture-Compile-Resolve" Workflow
Insight follows a professional 3-step lifecycle:
- CAPTURE (Local): While you develop, Insight records violations in the background. You can see them in your terminal or via the Browser UI Overlay.
- COMPILE (CI/Build): Before deploying, you run
php artisan insight:compile. This takes all your developer "fixes" and squashes them into a high-speed PHP manifest. - RESOLVE (Production): In production, our
RuntimeResolverreads the manifest and "magically" eager-loads the missing relations with zero database tracing.
๐ฆ Installation
composer require clcbws/laravel-eloquent-insight --dev
Note: The resolution engine is production-safe, but the recording tools are strictly for development.
๐ ๏ธ Usage (CLI & Terminal)
3. Clear the Slate
If you want to start a fresh audit session, clear the historical logs:
php artisan insight:clear
4. Audit & Fix
Once violations are captured, run the analysis:
# List all captured N+1 violations php artisan insight:list # Get a high-level efficiency audit (Hydration, Duplicates, Ghost Relations) php artisan insight:audit # Automatically fix N+1 violations php artisan insight:fix --all
๐ Production Deployment
For maximum performance in production, compile your detected optimizations into a static manifest. This converts dynamic analysis into an O(1) lookup:
php artisan insight:compile
This will generate storage/framework/insight/manifest.json, which is then used by the runtime engine with zero overhead.
Smart Auto-Fixer (The Wizard)
To have Insight automatically refactor your source code to add missing with() calls:
php artisan insight:fix --all
๐ฅ๏ธ UI Integration (Controllers, Livewire, Blade)
๐จ Browser UI Overlay
In your local environment, a premium Shadow-DOM overlay is injected into your HTML responses.
- Real-time Alerts: An "Insight" button appears in the bottom-right corner when issues are detected.
- Visual Breakdown: View N+1 violations, Ghost relations, and Duplicate queries without leaving your browser.
- One-Click Fixes: Apply AST-based source code refactoring directly from the overlay.
๐ฎ Controller Usage
Perfect for building custom admin APIs or JSON health endpoints.
use EloquentInsight\Facades\Insight; public function checkEfficiency() { $solutions = Insight::getLatestSolutions(); $efficiency = Insight::getEfficiencyReport(); return response()->json([ 'score' => Insight::getPerformanceScore(), 'bottlenecks' => $solutions, 'ghost_relations' => $efficiency['ghost_relations'], 'duplicates' => Insight::getDuplicateReport(), ]); }
โก Livewire Integration
Build a real-time "Eloquent Health" indicator for your admin panel.
namespace App\Livewire; use Livewire\Component; use EloquentInsight\Facades\Insight; class PerformanceMonitor extends Component { public function render() { return view('livewire.performance-monitor', [ 'solutions' => Insight::getLatestSolutions(), 'score' => Insight::getPerformanceScore(), 'efficiency' => Insight::getEfficiencyReport(), ]); } }
๐ Blade Templates
Quickly show an alert to administrators if performance regressions are detected in local dev.
@if(app()->isLocal() && Insight::getPerformanceScore() < 90) <div class="alert alert-warning"> <strong>๐ง Insight Notice:</strong> Your database efficiency has dropped to {{ Insight::getPerformanceScore() }}%. Run <code>php artisan insight:audit</code> to review. </div> @endif
๐๏ธ Architecture
- Collector: Low-level event listeners for Eloquent events.
- Heuristic Engine: Pattern-matching logic to identify entry points.
- AST Fixer: Safe PHP code modification via Abstract Syntax Trees.
- O(1) Resolver: High-speed manifest loader for production.
๐ Roadmap
Future versions of Eloquent Insight will focus on expanding the auto-fix capabilities:
- Automated Hydration Optimization: Automatically inject
->select()calls based on detected attribute access patterns. - Duplicate Query Refactoring: Identify redundant logic blocks and suggest caching or result-reuse strategies.
- Custom Heuristic Rules: Allow developers to define their own performance "red lines."
- In-Browser IDE Links: Click a violation in the audit report to jump directly to the line in VS Code/PhpStorm.
๐ Comparison: Why Insight Wins
| Feature | Standard Trackers (DebugBar/Clockwork) | Laravel Eloquent Insight |
|---|---|---|
| Production Overhead | ๐ข High (Continuous Tracing) | โก Zero (Static Resolver) |
| Resolution | ๐ฃ Manual Fixing Only | ๐ง Auto-Fix (AST Engine) |
| Analysis | ๐ Flat List of Queries | ๐ Impact Scoring (Weighted) |
| Optimization | โ None | ๐ฆ O(1) Compiled Manifest |
| Visuals | ๐ Generic Tables | ๐บ๏ธ Mermaid Topology Graphs |
โ๏ธ Advanced Configuration
Beyond the basic toggles, you can fine-tune the engine in config/insight.php:
ignore_namespaces
Prevent Insight from analyzing internal framework traces. This keeps your reports focused on your code.
'ignore_namespaces' => [ 'Illuminate\\', 'EloquentInsight\\', 'Sentry\\', // Ignore third-party packages ],
storage_path
Custom location for the violation buffer and exported reports.
'storage_path' => storage_path('framework/insight'),
๐ก๏ธ Production Security & Gating
We take production stability seriously. Eloquent Insight uses a dual-layer gate:
- Physical Gating: The
ViolationInterceptorandEfficiencyInterceptorstrictly check\app()->isLocal()before registering any listeners. - Feature Gating: All recording logic is disabled if
INSIGHT_ENABLEDis false in your.env.
In production, only the Runtime Resolver is active, performing a single O(1) array lookup per request. No traces are generated, and no files are written.
๐ ๏ธ Troubleshooting
"The UI Overlay isn't appearing"
- Ensure
INSIGHT_UI_ENABLED=trueis set in your.env. - Verify you are in the
localenvironment (APP_ENV=local). - Insight only injects into standard HTML responses (it skips JSON/API calls).
"Permission denied in storage"
Insight needs to write to storage/framework/insight/. Ensure this directory is writable by your web server:
mkdir -p storage/framework/insight/reports chmod -R 775 storage/framework/insight
๐ค Credits
- Author: Ahtesham
- Company: Broadway Web Service