Giter VIP home page Giter VIP logo

laravel-n-plus-one-detector's Introduction

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+.

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', '[email protected]'), // also possible: '[email protected],[email protected]'

    // 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', '[email protected]'), // also possible: '[email protected],[email protected]'

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 :)

laravel-n-plus-one-detector's People

Contributors

saasscaleup avatar

Stargazers

-/:;()$&@“.,?!’ avatar Ahmed Helal avatar Gerben Op den Kamp avatar Dicki Darmawan Saputra avatar Deniz Tezcan avatar Pavel Uvarov avatar Zulfikar Ditya avatar Mohamed AlQady avatar Nima HeydariNasab avatar Raymund C. Delfin avatar No name avatar Harun Baş avatar Hardik Poojara avatar maxtc avatar  avatar Mehran Rasulian avatar  avatar Matheus Ferreira  avatar Braunson Yager avatar Miftah Shidiq avatar Laode Muhammad Al Fatih avatar Rohan Chhabra avatar  avatar

Watchers

C.E.O. Ogego avatar  avatar

laravel-n-plus-one-detector's Issues

Issue on Installation

This message in the terminal shows up when I try to install the package

also, the config file is not published

Screenshot from 2024-07-10 10-25-41

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.