Debug Laravel Application with Laravel Debugbar example; In this tutorial we are going to share how to install and use Laravel Debugbar for debuging laravel application. The Laravel Debugbar by Barry vd. Heuvel is a package that allows you to quickly and easily keep tabs on your application during development.
This is a package to integrate PHP Debug Bar with Laravel. It includes a ServiceProvider to register the debugbar and attach it to the output. You can publish assets and configure it through Laravel. We have already share how to unable and disable laravel debugbar easy way.
Note: Use the DebugBar only in development. It can slow the application down (because it has to gather data). You can use this example in your laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 version.
Follow the following steps to install and use Laravel Debugbar for debugging laravel application queries;
Step 1: Install & Configure the Package
First install the package following command:
composer require barryvdh/laravel-debugbar --dev
To Enable laravel-debugbar, we have to change APP_DEBUG value to true.
Now add the debugbar to your providers and Facade manually in app\Providers\AppServiceProvider.php:
'providers' => [
....
Barryvdh\Debugbar\ServiceProvider::class,
],
'aliases' => [
....
'Debugbar' => Barryvdh\Debugbar\Facade::class,
]
Now publish the service provider of the package:
php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"
Step 2: Use Laravel Debugger
Now you can use the laravel debugger with add messages using the Facade (when added), using the PSR-3 levels (debug, info, notice, warning, error, critical, alert, emergency):
Debugbar::info($object);
Debugbar::info([$array]);
Debugbar::error('Error!');
Debugbar::warning('Watch out…');
Debugbar::addMessage('Another message', 'mylabel');
And start/stop timing:
To start or stop timing use like below:
Debugbar::startMeasure('render','Time for rendering'); Debugbar::stopMeasure('render'); Debugbar::addMeasure('now', LARAVEL_START, microtime(true)); Debugbar::measure('My long operation', function() { // Do something… });
Or log exceptions:
If you want to in try catch then you can use debugbar to throw the message.
try {
throw new Exception('foobar');
} catch (Exception $e) {
Debugbar::addThrowable($e);
}
Step 3: Output Laravel Debugger
After the all using above you can see the output of laravel debugger like below in your browser.
To know more about this package, please have a look at Laravel Debugbar official repository.