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.
Installation:
composer require barryvdh/laravel-debugbar --dev
Publish the config file:
php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider" --tag="config"
Enable Debug Mode:
Set APP_DEBUG=true in your .env file.
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.
Debugbar::info(), Debugbar::error(), or the debug() helper to log data.start_measure(), stop_measure(), or measure() to profile code execution.Debugging Queries:
Logging Custom Data:
Debugbar::info(['user_id' => auth()->id(), 'action' => 'login']);
debug('Custom message', $variable);
Profiling Performance:
Debugbar::startMeasure('render_view');
// Code to profile...
Debugbar::stopMeasure('render_view');
measure() for one-liners:
Debugbar::measure('long_operation', function() {
// Heavy operation...
});
Handling Exceptions:
try {
// Risky code...
} catch (\Exception $e) {
Debugbar::addThrowable($e);
}
AJAX Requests:
capture_ajax: true in config/debugbar.php to track AJAX calls.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',
],
],
Performance Overhead:
'collectors' => [
'queries' => false, // Disable if not needed
]
Storage Security:
storage.open in production. It exposes request history to unauthorized users.'storage' => [
'open' => function ($request) {
return $request->ip() === '127.0.0.1';
},
]
AJAX Auto-Show:
ajax_handler_auto_show if AJAX requests cause UI flickering:
'ajax_handler_auto_show' => false,
Editor Links Configuration:
remote_sites_path and local_sites_path are correctly mapped for editor links to work. Misconfigurations may break navigation.'editor_links' => [
'remote_sites_path' => [
'~/code/myapp' => '/myapp',
],
],
Collector Conflicts:
logs, cache) may duplicate data. Disable redundant collectors:
'collectors' => [
'logs' => false, // If using the 'messages' collector
]
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
Clear Storage:
Delete storage/debugbar to reset request history (for file driver).
Check for Errors:
APP_DEBUG=true is set.vscode:// or phpstorm:// protocol.Custom Collector Debugging:
DebugBar\DataCollector\Renderable for custom UI.Debugbar::getData() to inspect collected data:
dd(Debugbar::getData());
ServerTiming Headers:
Enable add_ajax_timing to integrate with Chrome DevTools:
'add_ajax_timing' => true,
Dark/Light Mode:
Force a theme via .env:
DEBUGBAR_THEME=dark
Livewire Integration:
Enable the livewire collector to debug Livewire components:
'collectors' => [
'livewire' => true,
]
Environment-Specific Config: Use environment variables to toggle features:
DEBUGBAR_COLLECT_JOBS=true
'collectors' => [
'jobs' => env('DEBUGBAR_COLLECT_JOBS', false),
]
Custom Storage Drivers:
Implement DebugBar\Storage\StorageInterface for custom storage (e.g., Redis):
'storage' => [
'driver' => 'redis',
'connection' => 'cache',
]
Leveraging Editor Links:
debugbar:// protocol for seamless navigation.How can I help you explore Laravel packages today?