maxis/laravel-eloquent-guard
Adds a lightweight guard layer to Laravel Eloquent models, helping protect sensitive attributes and control which fields can be mass-assigned or updated. Simple to configure and drop into existing models for safer, more predictable data handling.
This is a small, low-adoption package (21 stars, no dependents), so treat the README and source as the source of truth before wiring it into anything important. The happy path is short:
composer require maxis/laravel-eloquent-guard
Laravel's package discovery should register the service provider automatically. Then publish and inspect the config:
php artisan vendor:publish --provider="Maxiz88\\EloquentGuard\\EloquentGuardServiceProvider"
Check config/eloquent-guard.php (or whatever the published filename is) for the enable flag, slow-query threshold, and N+1 detection toggle. Enable it in local and staging first — not production — and drive one slow page or one feature test through it.
Where to look first:
vendor/maxis/laravel-eloquent-guard/src/ — confirm how it hooks into the query event and where it writes findings.config/logging.php — most lightweight monitors report through a log channel, so make sure the channel it targets actually exists.First use case: open a page you suspect is query-heavy, then tail the log. If N+1 detection works, you should see grouped/repeated query signatures or a warning naming the offending model/relation. That single loop — hit page, read finding, add with() — is the core value.
Environment-scoped activation. Keep it on in local, staging, and CI; off (or heavily sampled) in production. Runtime query listeners add overhead per query, and a monitor that fires on every request in production can generate more noise than signal.
// config/eloquent-guard.php (shape will vary — verify against README)
'enabled' => env('ELOQUENT_GUARD_ENABLED', app()->environment(['local', 'staging'])),
'slow_query_threshold_ms' => env('ELOQUENT_GUARD_SLOW_MS', 200),
'detect_n_plus_one' => true,
CI as a regression gate. The highest-leverage pattern for a package like this is running it during feature tests. If it exposes a throw/exception mode, enable that in phpunit.xml so a newly introduced N+1 fails the build instead of quietly shipping:
<env name="ELOQUENT_GUARD_ENABLED" value="true"/>
<env name="ELOQUENT_GUARD_THROW" value="true"/>
If it only logs, wrap your test assertions around the log or add a small custom assertion in your base TestCase.
Pair with a profiler, don't replace one. Use this as the always-on tripwire and Telescope/Debugbar/Blackfire for drill-down when it fires. The guard tells you that a request has an N+1; the profiler tells you which line and how to fix it.
Threshold tuning per environment. Staging on slower hardware will trip slow-query warnings that local won't. Set the threshold from an env var and override it in staging/CI rather than hardcoding. Keep N+1 detection aggressive (it's structural) and slow-query thresholds conservative (they're hardware-dependent).
Exclusion list for known-noisy queries. Health checks, session writes, migration runs, and queue polling produce repetitive queries that can look like N+1. Whatever ignore/except mechanism the package offers, populate it early — otherwise the first week of output is mostly false positives.
Refactor workflow. When you touch a controller or resource, run the relevant endpoint with the guard on and diff the query count before/after. It's a cheap way to prove an eager-loading change actually removed queries rather than just moving them.
composer.json.DB::listen means multiple listeners per query. If you see duplicated findings or unexpected slowdowns, disable the others while diagnosing.php artisan config:clear (or config:cache in deployed environments). A stale cached config is the usual reason "the enable flag isn't working."EXPLAIN, index checks, and a real profiler. Don't treat a clean guard report as proof of performance.composer.json before adopting; small packages often lag behind major framework releases.How can I help you explore Laravel packages today?