The Fastest Way of Debugging Laravel Application using Laravel Debugbar

Laravel Debugbar is a must-have package for those laravel developers who are looking for a better and alternate way of easy debugging insisted of navigating to error logs files or using the function like dd() and ddd() for inspecting data.

This package does more than just debugging it has the amazing capability of show application status data which are more difficult to inspect and also a deep look at models and queries executed with precise execution time this package is definitely a recommended.

Install barryvdh/laravel-debugbar

Goto your laravel app through the terminal and enter below command.

composer require barryvdh/laravel-debugbar --dev

You will see the output shown in the below image which means Laravel Debugbar has installed successfully.

Laravel Debugbar - Installing via composer package manager

Now there are some steps First;y, modify config/app.php and append Barryvdh\Debugbar\ServiceProvider::class and 'Debugbar' => Barryvdh\Debugbar\Facade::class at the bottom as shown.

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

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

Copy package config file and publish it into config folder.

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

This command will create a config file for the debugger in the folder config with the filename as debugbar.php. These config files are very used in customizing or altering the package as per for requirements.
You’ll also see the usage of config files in the below examples as you go through.

Laravel Debugbar preview

Attach debug messages into debugger Message tab

You have used laravel file error logging Log parallelly you can use message logging of this package as this is a great feature that lets you seen debug messages in the console itself insisted manually going to laravel.log file.

Laravel Debugbar provides 8 PSR-3 levels of error messages and they are debug, info, notice, warning, error, critical, alert, emergency.

You are also free to add your own message label using the addMessage($variable, 'lable_name') method.

Route::get('/', function () {
    $arr = [1,2,3];
    
    Debugbar::debug($arr);
    Debugbar::info($arr);
    Debugbar::notice($arr);
    Debugbar::warning($arr);
    Debugbar::error($arr);
    Debugbar::critical($arr);
    Debugbar::alert($arr);
    Debugbar::emergency($arr);        
    
    Debugbar::addMessage($arr, 'customized-label');
    
    return '';
}); 

Udemy Laravel Course for Beginners
Udemy Laravel Course for Beginners

Enable debugger in production mode

Here you have two ways where you can enable laravel debugger in production mode and also conditionally enable based on specific events or cases.
First look at enabling debugger in production mode for that goto .env file and add the below-given line.

DEBUGBAR_ENABLED=true

The DEBUGBAR_ENABLED takes a boolean value and assigning true will show debug bar similarly false will hide debug bar. Debugbar will show irrespective of production and whether APP_DEBUG is set to true or false.

Conditional enabling and disabling laravel debugger

There are two methods one is \Debugbar::disable(); and another is \Debugbar::enable();. The method \Debugbar::disable(); will hide or disable debug bar and the method \Debugbar::enable(); will show or enable debug bar.

// web.php    
Route::get('register', function () {
    \Debugbar::disable();
    return "view";
});

// in controller
public function index(){
    \Debugbar::disable(); // or \Debugbar::enable();
    return view('...', []);
}

Note

These methods can be called from part of application some cases are :

  • On a specific route.
  • On a group of routes using Route::group.
  • Can be toggled from controllers and middlewares

Pass variables to be debugged in Debugbar

The function debug() comes as a very effective tool to inspect data in the message console section. The data or variable may belong to views or used internally and it takes any type of data as an argument and that data will be visible in the console and they can be called in views using {{ debug($courses) }}.

The debug() a function is certainly better than using the laravel built-in dd() or ddd() functions as these function prevent the application from executing further logic.

Display variables in message console

Laravel Debugbar under the hood uses PHP Debug Bar. So take a time to look at PHP Debug Bar documentation.

Conclusion

In Conclusion, you have reached the end of this post on The Fastest way of Debugging Laravel Application using Laravel Debugbar. For suggestions and tutorial recommendation comment below and also don’t forget to share this post.

 

Related Posts

Summary
Review Date
Reviewed Item
The Fastest Way of Debugging Laravel Application using Laravel Debugbar
Author Rating
51star1star1star1star1star
Software Name
Laravel Debugbar
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Web Development