debugging using debugbar in laravel

Debugging is a helpful feature for developers to identify the causes of issues. Now days it's easy to debug applications because most of the modern application frameworks provide you inbuilt debug functionality. The Symfony Framework implements by default an useful debugging bar.

Laravel Debugbar 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. It bootstraps some Collectors to work with Laravel and implements a couple custom Data Collectors, specific for Laravel.

Install & Config The Laravel Debugbar Package

To install this package, enter below command into your terminal :

    
composer require barryvdh/laravel-debugbar
    

Before using this package you need to enable debugging for your application. so open .env file and make below change :

    
APP_DEBUG=true
    

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

config\app.php :

    
'providers' => [
    ....
    Barryvdh\Debugbar\ServiceProvider::class,
],
'aliases' => [
    ....
    'Debugbar' => Barryvdh\Debugbar\Facade::class,
]
    

After adding this package into application configuration. you need to publish related files to your application. Open terminal and enter below command :

    
php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"
    

Note : If you are facing any errror you might need clear configuration caches.

Using Laravel Debugbar

Now you can start debugging. you can use static methods to display messages for debug, info, notice, warning, error, critical, alert, emergency. Let's add messages to home page to check our setup :

routes\web.php

    
<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    \Debugbar::info("Hello World !");
    \Debugbar::error('Something went wrong');
    return view('welcome');
});
    

Now it will show two messages to debugbar's messages panel one as info and second as error. There are serval methods defined below :

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

// 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…
});
    

Enabling/Disabling on run time

Sometimes you need to enable or disable the Debugbar during run time. You can use below methods to do that :

    
\Debugbar::enable();
\Debugbar::disable();
    

Test Installation

As you already set test messages into main route, you just need to run it into browser using below command

    
php artisan serve
    

Open this URL into browser and it will show location information.

    
http://127.0.0.1:8000
    

Note : Use the DebugBar only in development. Because it can reveal sensitive information and slow down your Laravel application.


Share your thoughts

Ask anything about this examples