barryvdh/laravel-debugbar
Laravel Debugbar integrates PHP Debug Bar into Laravel, showing request, queries, views, routes, logs, exceptions, and performance details in a handy toolbar. Great for local debugging and profiling; configurable and easy to enable/disable per environment.
Installation:
composer require barryvdh/laravel-debugbar --dev
Add to config/app.php under providers:
Barryvdh\Debugbar\ServiceProvider::class,
Enable Debug Mode:
Set APP_DEBUG=true in .env or enable via:
debugbar()->enable();
First Use Case: Dump variables in real-time:
debug($user, $request->input());
View the debug bar at the bottom of your browser after a request.
Debugging Variables:
Replace dd() or dump() with debug() for non-breaking inspection:
debug($user->roles); // Appears in "Messages" tab
Timing Operations:
Measure execution time with measure():
debugbar()->measure('render-view', function() {
return view('dashboard');
});
Database Queries:
Enable db collector in config/debugbar.php to log queries with:
Exception Handling: Log uncaught exceptions automatically or manually:
try { ... } catch (\Exception $e) {
debugbar()->addThrowable($e);
}
Conditional Debugging:
Disable debugbar in production via .env:
APP_DEBUG=false
Or runtime:
debugbar()->disable();
Custom Collectors: Extend functionality by adding collectors (e.g., for third-party services):
debugbar()->addCollector(new \App\Debugbar\CustomCollector());
Console Usage: Enable debugbar in Artisan commands:
debugbar()->enable();
// ... command logic ...
debugbar()->disable();
Livewire Integration: Automatically captures Livewire component events and state changes.
Performance Overhead:
config/debugbar.php (e.g., events, cache).soft_limit/hard_limit for queries to avoid memory bloat:
'db' => [
'soft_limit' => 50, // Stop capturing bindings after 50 queries
'hard_limit' => 200, // Ignore queries after 200
]
Storage Risks:
storage.open in production—it exposes request history.debugbar()->setStorageCallback(function ($request) {
return $request->ip() === '127.0.0.1';
});
Query Backtrace Exclusions:
'db' => [
'backtrace_exclude_paths' => [
'vendor/laravel/framework',
'vendor/doctrine',
],
]
Twig Debugging:
{{ dump(var) }} with {{ debug(var) }} to avoid rendering in production.Isolate Slow Queries:
Use slow_threshold to highlight slow queries:
'db' => [
'slow_threshold' => 50, // Mark queries >50ms as slow
]
EXPLAIN Queries:
Enable on-demand EXPLAIN for complex queries (experimental):
'db' => [
'explain' => ['enabled' => true],
]
Memory Profiling:
Enable the memory collector to track usage spikes:
'collectors' => [
'memory' => true,
]
Custom Messages: Categorize messages with labels:
debugbar()->addMessage('User created', 'auth', 'success');
Custom Collectors:
Implement DebugBar\DataCollector\DataCollectorInterface:
class MyCollector implements DataCollectorInterface {
public function collect() { ... }
public function getName() { return 'my_collector'; }
}
Override Templates: Publish and modify debugbar views:
php artisan vendor:publish --tag=debugbar.views
Hook into Events:
Listen for debugbar.collect events to inject data dynamically:
Event::listen('debugbar.collect', function ($debugbar) {
$debugbar->addCollector(new MyDynamicCollector());
});
Console Output: Redirect debugbar data to CLI for headless debugging:
debugbar()->setOutputBuffering(false);
How can I help you explore Laravel packages today?