Laravel 12 Install and Configure Laravel Debugbar

In this laravel 12 tutorial, titled “laravel 12 install and configure laravel debugbar” you will learn how to install and configure laravel debugbar step by step with example.

Debugging in any application is an essential part of every developer’s workflow. In Laravel, one of the most popular debugging tools is Laravel Debugbar, which provides detailed insights about requests, queries, models, and application performance.

you will learn how to install and configure Laravel Debugbar step by step. We will also cover how to use it effectively for debugging and why you should avoid enabling it in production.

What is Laravel Debugbar?

Laravel Debugbar integrates PHP Debug Bar with Laravel and auto-attaches a developer toolbar to rendered pages, exposing collectors for queries, views, routes, logs, cache, memory, timeline, and exceptions. It’s auto-discovered in modern Laravel, so install-and-use is typically all that’s needed.

Why Use Laravel Debugbar?

Find slow parts quickly: see total request time, memory usage, and a timeline of framework and application events to pinpoint slow code paths fast.

Database insights without logging noise: inspect executed SQL, bindings, execution times, duplicates, and N+1 issues directly in a Queries panel.

Understand Blade rendering: view which Blade templates rendered, partials included, and how long each took, helping optimize view composition.

Safer, faster debugging than dd(): log structured messages, warnings, and exceptions to the bar without breaking page flow or exposing dumps to users.

Route and middleware clarity: confirm the exact route, controller, action, and middleware stack that handled the request, reducing routing guesswork.

Request/session/context at a glance: examine headers, payloads, cookies, and session state to verify assumptions during feature work.

When to use Laravel Debugbar?

  • During local development: Turn it on while building new features, fixing old code, or checking why some pages are loading slowly.
  • For database issues: Helpful when you think there are too many queries (N+1 problem), missing indexes, or heavy eager loading slowing things down.
  • When pages load slowly: Use it to see if Blade templates, loops, or big chunks of data are making your views heavy.
  • For random errors and bugs: Instead of crashing the page, Debugbar can capture exceptions and logs so you can trace what went wrong.
  • To check routes and middleware: Quickly confirm which route, controller, or middleware is being used when facing 404/405 errors or access problems.
  • On staging (for a short time only): You can enable Debugbar on a test server to reproduce performance issues — but always turn it off right after for security.

Steps for Laravel 12 Install and Configure Laravel Debugbar

  • Step 1: Install Laravel 12 Project
  • Step 2: Install Laravel Debugbar Package
  • Step 3: Configure Laravel Debugbar
  • Step 4: Enable or Disable Laravel Debugbar
  • Step 5: Start Debugging

Step 1: Install Laravel 12 Project

If you don’t have a Laravel 12 project yet, create one using Composer:

composer create-project laravel/laravel laravel-debugbar-app
cd laravel-debugbar-app

If you already installed ,you can skip this step.

Step 2: Install Laravel Debugbar Package

Install the package using Composer:

composer require barryvdh/laravel-debugbar --dev

The –dev flag ensures Debugbar is only installed in your development environment and not included in your production environment.

Step 3: Configure Laravel Debugbar

With Laravel’s auto-discovery feature, the service provider is registered automatically. Once installed, If you want to customize settings then you can publish the configuration and assets by running:

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

This will create a file in: config/debugbar.php.

Read Also : Laravel 12 How to Get Client IP Address Example

Step 4: Enable or Disable Laravel Debugbar

Laravel Debugbar can be toggled directly from the .env file using the APP_DEBUG variable. Set it to true to enable Debugbar or false to disable it, as shown below:

APP_DEBUG=true

If you want to disable only the Debugbar while keeping Laravel’s default debugging enabled, you can use the DEBUGBAR_ENABLED environment variable. Set it to true to enable or false to disable Debugbar, regardless of the APP_DEBUG setting.

If settings don’t apply immediately, refresh configuration cache.

php artisan config:cache

Step 5: Start Debugging

After completing the installation and configuration, you can now start using Laravel Debugbar. Simply run your application in the browser, and you’ll see the debug bar displayed at the bottom of the page.

It will show you useful information such as:

  • Request and response details
  • Execution time and memory usage
  • Database queries and performance insights
  • Blade views and rendering time
  • Route and middleware information
Laravel Debugbar Database Query Preview
Laravel Debugbar Database Queries

With these insights, you can quickly identify performance issues, optimize queries, and debug errors more effectively. If you don’t see it, check that APP_DEBUG=true in your .env file.

Usage Of Debugbar

Once Debugbar is visible, you can also use it programmatically to log data, measure execution time, and handle errors.

Example: Logging and Timing

In this example, we use Debugbar::startMeasure() and Debugbar::stopMeasure() to measure how long a specific piece of code takes to execute.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Profile;
use Debugbar;

class UserController extends Controller
{
    public function index()
    {
        Debugbar::startMeasure('query', 'Time Showing for Quering');
        // Retrieve the user with their profile
        $userWithProfile = User::with('profile')->get();
        Debugbar::stopMeasure('query');
        
        return view('welcome',compact('userWithProfile'));

        
    }

Laravel 12 Install and Configure Laravel Debugbar
Laravel Debugbar Timeline

⚠️ Make sure to import the Debugbar class; otherwise, you may encounter errors. If you prefer not to import it, you can reference it directly like this:

   \Debugbar::stopMeasure('query');

Example: Enable/Disable at Runtime

You can turn Debugbar on or off conditionally:

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

If you add Debugbar::disable(); inside a specific controller, view, the Debugbar will only be disabled for that page while remaining active on others. However, if you place it inside a route, it will disable the Debugbar for all requests handled by that route, since a route is connected to both its controller and view.

This flexibility allows you to use Debugbar only when needed without affecting normal page execution.

Example: Logging Different Message Types with Debugbar

You can log different kinds of messages (info, warning, error, exception, models) using the Debugbar facade.

 Debugbar::error('Error! This is error');
 Debugbar::warning('Watch out…, This is warning');
 Debugbar::addMessage('Another message', 'mylabel');
Laravel Debugbar Error Message Preview
Laravel Debugbar Error Message Preview

Example: Exceptions with Debugbar

Exceptions are logged with full stack trace information, allowing developers to identify and resolve issues more quickly during debugging.

Laravel 12 Install and Configure Laravel Debugbar
Laravel Debugbar Exceptions

Example: Models With Debugbar

Keeps track of Eloquent models retrieved in the current request, displaying counts and performance metrics.

Laravel Debugbar Models Preview
Laravel Debugbar Models

Security Note

  • Use with care: Laravel Debugbar is a great tool, but it shouldn’t always be on. Use it wisely so you don’t leak sensitive info or slow down your live site.
  • Only for development: Keep Debugbar active only in local or staging environments. Control it with environment variables (like DEBUGBAR_ENABLED) so it doesn’t accidentally run in production.
  • Keep data safe: Debugbar can show queries, request data, and other details. Always turn it off in production to avoid exposing secrets or user information.

Conclusion

In this tutorial, you learned how to install and configure Laravel Debugbar in Laravel 12. With Debugbar, you can:

  • Track request performance, queries, and views.
  • Debug errors and exceptions safely.
  • Gain full visibility into routes, middleware, and session data.

By using Laravel Debugbar, you can speed up debugging, find bottlenecks, and write more efficient Laravel applications. Just remember to disable it in production to keep your application secure.