maxis / laravel-eloquent-guard
Runtime Eloquent performance monitor
Requires
- php: ^8.2
- illuminate/contracts: ^10.0|^11.0|^12.0
- illuminate/database: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- laravel/pulse: ^1.0
- livewire/livewire: ^3.0
- orchestra/testbench: ^10.9
- phpunit/phpunit: ^12.5
Suggests
- laravel/pulse: Required to use the dashboard monitoring card (^1.0).
- livewire/livewire: Required for the Pulse dashboard components (^3.0).
- sentry/sentry-laravel: Required to use the SentryReporter (^3.0|^4.0).
README
Eloquent Guard is a lightweight, production-ready runtime performance monitor for Laravel. It detects N+1 queries and slow database operations as they happen, helping you keep your application fast and efficient.
โจ Features
- N+1 Query Detection: Automatically detects repeated queries within a single request.
- Slow Query Monitor: Alerts you when a query exceeds your defined time limit.
- Laravel Pulse Integration: Beautiful custom dashboard card to visualize alerts.
- Smart Backtrace: Pinpoints the exact file and line of code causing the issue (skips vendor files).
- Multi-channel Reporting: Built-in support for Log, Slack, Telegram, and Sentry.
- Zero Configuration: Works out of the box with sensible defaults.
๐ข Multi-Channel Reporting
Stay informed where your team already works. All alerts include a Smart Backtrace that points directly to your code, skipping the vendor/ noise.
๐ Installation
You can install the package via composer:
composer require maxis/laravel-eloquent-guard
Usage
Publish Configuration
Publish the configuration file to customize thresholds, ignored tables, and active reporters:
php artisan vendor:publish --provider="Maxis\EloquentGuard\EloquentGuardServiceProvider" --tag="config"
Configure Environment
Add your webhook URLs or bot tokens to your .env file for instant alerts:
ELOQUENT_GUARD_ENABLED=true ELOQUENT_GUARD_SLACK_WEBHOOK=https://hooks.slack.com... ELOQUENT_GUARD_TELEGRAM_TOKEN=your-bot-token ELOQUENT_GUARD_TELEGRAM_CHAT_ID=your-chat-id
Important
Queue Worker Required: To ensure your application's performance is not affected, reporting (Slack, Telegram, Sentry) is handled via Laravel's queue system. Make sure your queue worker is running (php artisan queue:work) to receive alerts.
โ๏ธ Configuration
Once published, you can find the settings in config/eloquent-guard.php.
Thresholds & Limits
Customize what constitutes a performance issue for your specific application:
'limits' => [ 'n_plus_one_threshold' => 5, // Alert after 5 identical queries in one request 'query_duration_ms' => 500, // Alert if a single query takes more than 500ms ],
Reporters
Enable or disable notification channels by adding/removing classes from the reporters array:
'reporters' => [ \Maxis\EloquentGuard\Reporters\LogReporter::class, // Standard Laravel Logs \Maxis\EloquentGuard\Reporters\SlackReporter::class, // Slack Webhooks (Queue Worker Required) \Maxis\EloquentGuard\Reporters\TelegramReporter::class, // Telegram Bot API (Queue Worker Required) \Maxis\EloquentGuard\Reporters\SentryReporter::class, // Sentry Issue Tracking (Queue Worker Required) ],
Ignored Tables
Prevent noise from system tables (like sessions or cache):
'except_tables' => ['sessions', 'cache', 'pulse_entries', 'jobs', 'migrations'],
๐ Laravel Pulse Integration
Eloquent Guard comes with a dedicated Laravel Pulse card to visualize N+1 and Slow Query trends directly in your dashboard.
Add the Card to Dashboard
Include the Livewire component in your resources/views/vendor/pulse/dashboard.blade.php:
<x-pulse> <!-- Display Eloquent Guard Alerts --> <livewire:maxis.eloquent-guard cols="4" /> <!-- Existing Pulse cards... --> </x-pulse>
๐งช Testing
The package is fully tested with PHPUnit. If you are developing locally or using Laravel Sail, run the test suite with:
# Using Laravel Sail sail php ./packages/maxis/eloquent-guard/vendor/bin/phpunit -c ./packages/maxis/eloquent-guard/phpunit.xml # Or standard PHPUnit from the package directory ./vendor/bin/phpunit
๐ค Contributing
Contributions make the open-source community amazing!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
๐ License
Distributed under the MIT License. See LICENSE.md for more information.



