Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Debugbar Laravel Package

barryvdh/laravel-debugbar

Laravel Debugbar integrates PHP Debug Bar into Laravel, showing request timing, queries, routes, views, cache/events, and more. Easy install and configurable collectors with support for AJAX and profiling to help diagnose performance issues in development.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require barryvdh/laravel-debugbar --dev
    

    Publish the config file:

    php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider" --tag="config"
    
  2. Enable Debug Mode: Set APP_DEBUG=true in your .env file.

  3. First Use Case: Trigger a request (e.g., visit a route or run a command). The debug bar will appear at the bottom of the page, displaying default collectors like Queries, Messages, Views, and Route.

    New Feature: SQL query backtrace entries now include editor links (e.g., IDE links) for direct navigation to the query's source code.


Key Starting Points

  • Viewing Data: Click tabs in the debug bar to inspect queries, logs, or request data.
  • Custom Messages: Use Debugbar::info(), Debugbar::error(), or the debug() helper to log data.
  • Timing Operations: Use start_measure(), stop_measure(), or measure() to profile code execution.
  • SQL Query Debugging: Click on query entries in the Queries tab to jump directly to the relevant code in your IDE (if configured).

Implementation Patterns

Core Workflows

  1. Debugging Queries:

    • Enable the Queries collector (default) to inspect SQL queries and execution times.
    • New Feature: Click on a query to open the corresponding file and line in your IDE (e.g., VS Code, PHPStorm) via editor links.
    • Example: Identify slow queries by sorting the Queries tab by duration.
  2. Logging Custom Data:

    • Use the Messages collector to log variables or objects:
      Debugbar::info(['user_id' => auth()->id(), 'action' => 'login']);
      
    • Or via helpers:
      debug('Custom message', $variable);
      
  3. Profiling Performance:

    • Measure execution time for critical sections:
      Debugbar::startMeasure('render_view');
      // Code to profile...
      Debugbar::stopMeasure('render_view');
      
    • Use measure() for one-liners:
      Debugbar::measure('long_operation', function() {
          // Heavy operation...
      });
      
  4. Handling Exceptions:

    • Log exceptions manually:
      try {
          // Risky code...
      } catch (\Exception $e) {
          Debugbar::addThrowable($e);
      }
      
  5. AJAX Requests:

    • Enable capture_ajax: true in config/debugbar.php to track AJAX calls.
    • View history via the folder icon in the debug bar.

Integration Tips

  • Conditional Debugging: Disable collectors in production or for specific environments:

    if (app()->environment('local')) {
        debugbar()->enable();
    }
    
  • Custom Collectors: Extend functionality by creating a custom collector:

    use DebugBar\DataCollector\DataCollector;
    use DebugBar\DataCollector\Renderable;
    
    class MyCollector implements DataCollector, Renderable {
        public function collect() { /* Logic */ }
        public function getWidgets() { /* Render */ }
    }
    

    Register it via the facade:

    Debugbar::addCollector(new MyCollector());
    
  • Console Usage: Enable debug bar for CLI commands:

    use Barryvdh\Debugbar\Facades\Debugbar;
    
    public function handle() {
        Debugbar::enable();
        // Command logic...
    }
    
  • Twig Integration: Replace dump() with debug() in Twig templates:

    {{ debug(user) }}
    
  • Editor Links for Queries: Ensure your IDE supports editor links (e.g., VS Code, PHPStorm) and configure remote_sites_path and local_sites_path in config/debugbar.php for seamless navigation:

    'editor_links' => [
        'remote_sites_path' => [
            '~/Projects/myapp' => '/myapp',
        ],
        'local_sites_path' => [
            '/var/www/myapp' => '/myapp',
        ],
    ],
    

Gotchas and Tips

Common Pitfalls

  1. Performance Overhead:

    • Debugbar slows down requests due to data collection. Disable unused collectors in production:
      'collectors' => [
          'queries' => false, // Disable if not needed
      ]
      
  2. Storage Security:

    • Never enable storage.open in production. It exposes request history to unauthorized users.
    • Use IP-based callbacks to restrict access:
      'storage' => [
          'open' => function ($request) {
              return $request->ip() === '127.0.0.1';
          },
      ]
      
  3. AJAX Auto-Show:

    • Disable ajax_handler_auto_show if AJAX requests cause UI flickering:
      'ajax_handler_auto_show' => false,
      
  4. Editor Links Configuration:

    • Ensure remote_sites_path and local_sites_path are correctly mapped for editor links to work. Misconfigurations may break navigation.
    • Example for Docker/Homestead:
      'editor_links' => [
          'remote_sites_path' => [
              '~/code/myapp' => '/myapp',
          ],
      ],
      
  5. Collector Conflicts:

    • Some collectors (e.g., logs, cache) may duplicate data. Disable redundant collectors:
      'collectors' => [
          'logs' => false, // If using the 'messages' collector
      ]
      

Debugging Tips

  1. Disable Collectors Temporarily: Use runtime disable/enable to isolate issues:

    debugbar()->disable(); // Disable all collectors
    Debugbar::info('Test'); // Won't appear
    debugbar()->enable();  // Re-enable
    
  2. Clear Storage: Delete storage/debugbar to reset request history (for file driver).

  3. Check for Errors:

    • Ensure APP_DEBUG=true is set.
    • Verify no JavaScript errors block the debug bar (check browser console).
    • For editor links, ensure your IDE supports the vscode:// or phpstorm:// protocol.
  4. Custom Collector Debugging:

    • Implement DebugBar\DataCollector\Renderable for custom UI.
    • Use Debugbar::getData() to inspect collected data:
      dd(Debugbar::getData());
      

Advanced Tips

  1. ServerTiming Headers: Enable add_ajax_timing to integrate with Chrome DevTools:

    'add_ajax_timing' => true,
    
  2. Dark/Light Mode: Force a theme via .env:

    DEBUGBAR_THEME=dark
    
  3. Livewire Integration: Enable the livewire collector to debug Livewire components:

    'collectors' => [
        'livewire' => true,
    ]
    
  4. Environment-Specific Config: Use environment variables to toggle features:

    DEBUGBAR_COLLECT_JOBS=true
    
    'collectors' => [
        'jobs' => env('DEBUGBAR_COLLECT_JOBS', false),
    ]
    
  5. Custom Storage Drivers: Implement DebugBar\Storage\StorageInterface for custom storage (e.g., Redis):

    'storage' => [
        'driver' => 'redis',
        'connection' => 'cache',
    ]
    
  6. Leveraging Editor Links:

    • Configure your IDE to recognize the debugbar:// protocol for seamless navigation.
    • Example for VS Code: Add a custom URI handler or use extensions like "URI Handler" to open files directly from the debug bar.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport