saasscaleup/laravel-n-plus-one-detector

Real-time detection and resolution of N+1 query issues for Laravel applications. Support now Laravel 11

v1.0.6 2024-10-14 05:20 UTC

This package is auto-updated.

Last update: 2024-12-14 05:40:34 UTC


README

Laravel N+1 Query Detector

Real-time detection and resolution of N+1 query issues for Laravel applications. Provides detailed insights, advanced notifications, and a rich admin dashboard.

Perfect for solo developers and teams. Compatible with Laravel 5.5+ and PHP 7+.

Youtube · Twitter · Facebook · Buy Me a Coffee

Latest Stable Version Total Downloads License


banner

Laravel N+1 Query Detector

Laravel N+1 Query Detector is a powerful package designed to help you identify and resolve N+1 query problems in real-time. Perfect for individual developers and teams, this package enhances your application’s performance by catching inefficient queries before they impact your users.

✨ Features

  • Real-time N+1 Query Detection: Identify N+1 queries as they happen, ensuring your application remains performant.
  • Detailed Query Insights: Get detailed information about each detected N+1 query, including file line, the class and methods involved.
  • Advanced Notifications: Receive alerts via Slack, webhooks, or email, ensuring you never miss an important notification.
  • Rich Admin Dashboards: View all N+1 warnings in a comprehensive and user-friendly dashboard.
  • Suit for Teams and Solo Developers: Designed to be used by both solo developers and teams working collaboratively.

Requirements

  • PHP >= 7
  • Laravel >= 5.5

Installation

Install composer package (dev)

Via Composer - Not recommended for production environment

composer require --dev saasscaleup/laravel-n-plus-one-detector

Publish package's config, migration and view files

Publish package's config, migration and view files by running below command:

Try this command first

php artisan vendor:publish --provider="Saasscaleup\NPlusOneDetector\NPlusOneDetectorServiceProvider"

If you are getting this result:

No publishable resources for tag [].
Publishing complete.

Than try to this:

php artisan vendor:publish

and then enter the number of that represent the provider (in the example below the number is 14)

  [13] Provider: Saasscaleup\LogAlarm\LogAlarmServiceProvider
  [14] Provider: Saasscaleup\NPlusOneDetector\NPlusOneDetectorServiceProvider

> 14

Run migration command

Run php artisan migrate to create nplusone_warnings table.

php artisan migrate

Run route:cache command

Run php artisan migrate to make sure route cache will be purge becasue we are adding new route /n-plus-one-dashboard.

php artisan route:cache

Configuration

You can configure the package by editing the config/n-plus-one.php file. This file allows you to set the threshold for detecting N+1 queries, notification preferences, and more.

<?php

return [
    
    // Whether or not to enable the N+1 Detector
    'enabled' => env('NPLUSONE_ENABLED', true),
    
    // The number of queries below which no alert will be triggered
    'queries_threshold' => env('NPLUSONE_QUERIES_THRESHOLD', 50),
    
    // The number of queries below which no detector will be triggered
    'detector_threshold' => env('NPLUSONE_DETECTOR_THRESHOLD', 10),
    
    // The number in minutes a n+1 query will be stored in memory before being discarded. So it won't repeat itself
    'cache_lifetime' => env('NPLUSONE_CACHE_LIFETIME', 14400), // 10 days
      
    // Slack webhook url for N + 1 Detector
    'slack_webhook_url' => env('NPLUSONE_SLACK_WEBHOOK_URL', ''),

    // Custom webhook url for N + 1 Detector
    'custom_webhook_url' => env('NPLUSONE_CUSTOM_WEBHOOK_URL', ''),

    // notification email address for N + 1 Detector
    'notification_email' => env('NPLUSONE_NOTIFICATION_EMAIL', 'admin@example.com'), // also possible: 'admin@example.com,admin2@example.com'

    // notification email subject for N + 1 Detector
    'notification_email_subject' => env('NPLUSONE_NOTIFICATION_EMAIL_SUBJECT', 'N+1 Detector Notification'),

    // Dashboard Middleware for N + 1 Detector
    'dashboard_middleware' => env('NPLUSONE_DASHBOARD_MIDDLEWARE', ['web', 'auth']),

    // Dashboard Pagination for N + 1 Detector
    'dashboard_records_pagination' => env('NPLUSONE_DASHBOARD_RECORDS_PAGINATION', 10),

];

Usage

Real-time Detection

The package automatically listens to your database queries and detects N+1 issues in real-time. When an N+1 query is detected, it logs the query details and optionally sends notifications.

Admin Dashboard

Access the rich admin dashboard to view all N+1 warnings:

Route::get('/n-plus-one-dashboard', [NPlusOneDashboardController::class, 'index'])->name('n-plus-one.dashboard');

The dashboard provides a comprehensive view of all detected N+1 queries, including SQL statements, occurrences, locations, and suggested fixes.

banner

Notifications

Configure notifications to be sent via Slack, webhook, or email. Set your notification preferences in the config/n-plus-one.php file to stay informed about N+1 issues in your application.

    // Slack webhook url for N + 1 Detector
    'slack_webhook_url' => env('NPLUSONE_SLACK_WEBHOOK_URL', ''),

    // Custom webhook url for N + 1 Detector
    'custom_webhook_url' => env('NPLUSONE_CUSTOM_WEBHOOK_URL', ''),

    // notification email address for N + 1 Detector
    'notification_email' => env('NPLUSONE_NOTIFICATION_EMAIL', 'admin@example.com'), // also possible: 'admin@example.com,admin2@example.com'

Slack notification

slack

Webhook notification

webhook

Email notification

email

Advanced Features

Detailed Query Insights

The package provides detailed insights into each detected N+1 query, including the class and methods involved. This helps you quickly pinpoint the source of the problem and implement a fix.

License

Please see the MIT for more information.

Support 🙏😃

If you Like the tutorial and you want to support my channel so I will keep releasing amzing content that will turn you to a desirable Developer with Amazing Cloud skills... I will realy appricite if you:

  1. Subscribe to our youtube
  2. Buy me A coffee ❤️

Thanks for your support :)