Debugging is an essential part of web development, and Laravel provides a powerful debugging tool called Laravel Debugbar, developed by Barryvdh. This package offers real-time debugging information, making it easier to identify performance issues, slow queries, route information, and more.
In this guide, we will explore Laravel Debugbar, how to install it, and how to leverage its features for optimizing Laravel applications.
Laravel Debugbar is an open-source debugging package that integrates with Laravel applications and provides a toolbar with debugging details. It helps developers track:
Executed SQL queries
Request and response information
Exceptions and errors
View templates
Timeline of execution
Routes and controllers
Previous requests
Mail and HTTP requests
And much more!
Now, let's see how to install and use Laravel Debugbar in your projects.
To install Laravel Debugbar, run the following command using Composer:
composer require barryvdh/laravel-debugbar --dev
After installation, Laravel Debugbar will be automatically registered for development environments. However, if needed, you can publish the configuration file using:
php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"
This will create a config/debugbar.php
file, where you can customize Debugbar settings.
The Timeline tab helps measure execution time and pinpoint bottlenecks in your code. You can manually add measurements like this:
Debugbar::startMeasure('render','Time for rendering'); Debugbar::stopMeasure('render'); Debugbar::addMeasure('now', LARAVEL_START, microtime(true)); Debugbar::measure('My long operation', function() { // Do something… });
If your Laravel application experiences slow performance, using the Timeline feature allows you to track exactly where delays are happening. For example, if a page takes too long to load, you can measure database queries, API calls, and view rendering times to determine the bottleneck.
The Exceptions tab logs any errors that occur in your application. You can manually add exceptions to Debugbar like this:
try { throw new Exception('foobar'); } catch (Exception $e) { Debugbar::addException($e); }
In large Laravel applications, unhandled exceptions can lead to crashes or incorrect data rendering. With Debugbar’s exception logging, developers can quickly identify issues and debug them before they impact users.
The Views tab provides insights into rendered templates and the data passed into them. This helps ensure only necessary data is sent to views, improving performance and maintainability.
If you notice a slow-loading page, checking the Views tab can reveal if unnecessary or redundant data is being passed, allowing you to optimize queries and reduce response times.
With the Route tab, you can see details about the current request, including:
URI
Controller
Middleware
File path
Namespace
When troubleshooting route-related issues, such as 404 errors or incorrect controller mappings, Debugbar provides immediate insights to resolve them efficiently.
The Queries tab logs all SQL queries executed during a request. This helps detect issues such as:
N+1 query problems
Unoptimized queries
Missing indexes
Example:
use Illuminate\Support\Facades\DB; DB::enableQueryLog(); $users = DB::table('users')->get(); Debugbar::info(DB::getQueryLog());
If a report page loads too slowly, Debugbar can highlight excessive or redundant queries. Developers can then optimize them by adding eager loading (with()
), caching, or indexing.
Laravel Debugbar also provides information about:
Emails sent from the application (Mail tab)
Current HTTP request data (Request tab)
If email notifications fail to send, Debugbar allows you to check email logs and verify if messages were sent correctly.
By clicking the Folder Icon in Debugbar, you can view previous requests, including:
AJAX requests
API responses
Session data
When debugging an API-driven Laravel application, previous request tracking helps analyze API request-response cycles, ensuring expected data is returned.
Although Laravel Debugbar is useful for development, it should not be used in production. You can disable it by setting DEBUGBAR_ENABLED=false
in the .env
file:
APP_DEBUG=false DEBUGBAR_ENABLED=false
Or, in config/debugbar.php
:
'response' => env('DEBUGBAR_ENABLED', false),
Use Debugbar only in local environments – Disable it in production to avoid exposing sensitive information.
Monitor query execution time – Optimize slow queries to improve performance.
Check previous requests for AJAX debugging – Helps troubleshoot asynchronous requests.
Track Blade templates – Ensure minimal and efficient data passing.
Analyze timeline data – Identify bottlenecks in Laravel applications.
Laravel Debugbar is an invaluable tool for Laravel developers, helping with performance optimization, error tracking, and debugging queries. By leveraging its powerful features, you can enhance your application's efficiency and improve debugging workflows.
composer require barryvdh/laravel-debugbar --dev
DEBUGBAR_ENABLED=false
in your .env
file.