Debug Laravel Application with Laravel Debugbar

In this article, I’m going to share how to debug Laravel application with laravel-debugbar. 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.

Note: Use the DebugBar only in development. It can slow the application down (because it has to gather data).

Table of Contents

  1. Install & Config The Package
  2. Usage
  3. Output

Install & Config The Package

Run this command to install the package:

composer require barryvdh/laravel-debugbar --dev

To enable debugbar, we have to change APP_DEBUG value to true.

If you don’t use auto-discovery, add the ServiceProvider & Facade manually:

'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"

Usage

You can now add messages using the Facade (when added), using the PSR-3 levels (debug, info, notice, warning, error, critical, alert, emergency):

Debugbar::info($object);
Debugbar::error('Error!');
Debugbar::warning('Watch out…');
Debugbar::addMessage('Another message', 'mylabel');

And start/stop timing:

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:

try {
    throw new Exception('foobar');
} catch (Exception $e) {
    Debugbar::addThrowable($e);
}

There are also helper functions available for the most common calls:

// All arguments will be dumped as a debug message
debug($var1, $someString, $intValue, $object);

// `$collection->debug()` will return the collection and dump it as a debug message. Like `$collection->dump()`
collect([$var1, $someString])->debug();

start_measure('render','Time for rendering');
stop_measure('render');
add_measure('now', LARAVEL_START, microtime(true));
measure('My long operation', function() {
    // Do something…
});

Output

After successful installation, you’ll see output like:

To know more about this package, please have a look at Laravel Debugbar official repository.

That’s all, artisans. Thanks for reading.