Paulund
2017-08-28 #laravel

Laravel Debugbar Package

The Laravel debug package is always the first package I install when starting a new Laravel project. This makes it so easy to debug any performance problems you have in your application.

It provides a detailed view of:

  • Timeline of the application
  • Exceptions thrown
  • Views used to display the page and includes used
  • Route information of the page including, controller used, middleware, uri, namespace etc
  • How many queries ran and exactly what queries ran.
  • Mails sent on page load
  • Authentication middleware used
  • Policy gate
  • Session information
  • Request object information
  • Size of the page in MB
  • Time is takes for the page to load

Why Is This Important?

The information that the debugbar returns to you is very important to how you develop your application. When you can accurately see how long your page takes and what queries are being ran you can make the necessary steps in improving the code quality of your application. I'm sure we've all worked on projects before where performance wasn't really taken into consideration at the start of development, they just wanted to get it working.

Then when you start to add more data into the database you find that it starts to run slower due to the impact on performance. Using the debugbar you can spot these problems early on during your development so that you'll know if you need to make any improvements to the application.

One of the most common areas I find takes a big hit on performance and somewhere which is hard to spot while developing is the eloquent lazy load functionality, this allows you to query the relationship of an entity from anywhere in the code. A common use of lazy loading for example is getting a list of users, displaying them in your blade template and then having a column to display all the roles. You could query the database once then on listing out the user table you could then query to get the roles, so we have a blade template that looks like the following.


<table>
  @foreach($users as $user)
    <tr>
      <td>{{ $user->name }}</td>
      <td>
        <ul>
          @foreach($user->roles as $role)
            <li>{{ $role->name }}</li>
          @endforeach
        </ul>
      </td>
    </tr>
  @endforeach
</table>

The problem with this is that for each user the application will make a new query to get the roles for this user. During development you might only have one user, therefore you'll be making 2 queries to display one user. This is something you'll be able to notice on the debugbar that your application is making one extra query than you expect. This isn't a problem during development but on production if you display 50 users you suddenly have over 50 queries to display a simple page. What if you want to display the capabilities of these roles, that's another query per role per user. This is why the Laravel debugbar is so important to use while developing. It helps catch problems that you may not notice exist.

How To Install Laravel Debugbar

To install Laravel debugbar it's as easy as pulling a composer package


composer require --dev barryvdh/laravel-debugbar

You should only use the debugbar in development as it can slow down your application so make sure you include the --dev flag. In Larave 5.5 there is auto package discovery but if you're on a lower version you'll need to register the package in your config/app.php file.


Barryvdh\Debugbar\ServiceProvider::class,

If you want to log messages to the debugbar then you can install the facade in the app.php file.


'Debugbar' => Barryvdh\Debugbar\Facade::class,

To learn more about the Laravel debugbar visit the github page. Laravel Debugbar On Github