Laravel 12 – Debugbar Track SQL Queries, Performance & Errors in Seconds.

Touseef Afridi
19 Dec 25

Laravel 12 – Debugbar Track SQL Queries, Performance & Errors in Seconds.

In this tutorial, we will learn how to integrate Debugbar in Laravel 12, a powerful tool for real-time debugging and profiling, helping developers optimize performance and troubleshoot issues efficiently.


If you're a video person, feel free to skip the post and check out the video instead!


Quick Overview

This guide walks through integrating Laravel Debugbar for debugging and performance monitoring in a Laravel 12 application. It begins with creating a fresh Laravel project or using an existing one and navigating into its directory. Next, the Debugbar package is installed via Composer, and its configuration file is published for customization. The guide then shows how to enable Debugbar only in the local environment to avoid running it in production. Sample users are created using Laravel Tinker, and the development server is started to test Debugbar in the browser. An example route fetching all users demonstrates how Debugbar captures and displays executed database queries in real time. Finally, the guide highlights Debugbar’s benefits, including SQL query monitoring, request timing, view rendering inspection, and exception logging, making it a powerful tool for optimizing Laravel applications.

Step # 1 : Create a Fresh Laravel 12 Application.

Before diving into any features, the first thing you’ll need is a clean Laravel 12 project. You can either start from scratch or continue inside an existing workspace. For this guide, we’ll assume you’re setting up a brand-new application. If you already have the Laravel Installer installed globally, creating a new project is quick and straightforward. Run the command below to generate a new Laravel 12 app named debugbar.
laravel new debugbar
Don’t have the Laravel Installer? No problem at all. You can achieve the same result using Composer.
composer create-project laravel/laravel --prefer-dist debugbar
When you create the project using Composer, Laravel skips any interactive questions and simply scaffolds a default application. You can always tweak the configuration later as needed. On the other hand, if you use the Laravel Installer, you’ll be guided through a short setup process. When prompted, choose the following options.
  • Starter Kit: Select None to keep the installation lightweight and framework-only.
  • Testing Framework: Choose Pest for a modern and expressive testing experience.
  • Database: Pick MySQL as your main database engine.
  • Run Migrations: Type Yes to apply the default migrations immediately.
  • Frontend Dependencies: Select Yes so Laravel installs the required frontend tooling automatically.

Once the setup is complete, Laravel will create a new debugbar directory, install all required dependencies, and prepare the database structure. With that done, your Laravel 12 project is ready and you can move on to building and customizing your application

Step # 2 : Move Into Your Laravel Project Directory.

Now that your Laravel 12 application has been created, the next step is to access its root folder. Open your terminal (for example, Git Bash on Windows) and navigate to the project directory.
cd c:/xampp/htdocs/debugbar
Once you’re inside the project folder, you can start running Laravel commands, managing dependencies, updating configuration files, and continuing with development tasks.

Step # 3 : Install the Laravel Debugbar Package.

To add Laravel Debugbar to your application, install the package using the command below.
composer require barryvdh/laravel-debugbar --dev
This installs Debugbar as a development only dependency, meaning it won’t be loaded in production. After installation, the package is automatically registered by Laravel, so no extra setup is needed. Once enabled, Debugbar displays a debugging toolbar inside your application. It shows useful information such as executed database queries, request timing, memory usage, and other helpful runtime details. This makes it easier to monitor what’s happening behind the scenes while developing your Laravel application.

Step # 4 : Publish the Debugbar Configuration File.

Next, publish the Debugbar configuration so you can control how it behaves in your application. Run the following Artisan command.
php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"
This will create a configuration file at config/debugbar.php. From there, you can customize Debugbar settings such as enabling or disabling features, controlling data storage, and deciding what information should be collected during requests. Having this configuration file allows you to fine-tune Debugbar based on your development needs while keeping production environments clean and optimized.

Step # 5 : Enable Debugbar in the Local Environment.

To make sure Debugbar runs only during development, update your .env file with the following values.
APP_ENV=local
APP_DEBUG=true
This setup ensures that Debugbar is active only in the local environment. The APP_ENV=local  & APP_DEBUG=true setting limits its usage to development. With this in place, Debugbar remains disabled in production, helping maintain better performance and security.

Step 6: Create Users Using Tinker.

Open Laravel Tinker to interact with your application from the command line.
php artisan tinker
Generate five users in the database using the model factory.
App\Models\User::factory()->count(5)->create();
Close Tinker and return to your normal terminal prompt.
exit
This will populate your database with users you can use for testing.

Step # 7 : Start the Development Server and Test Debugbar.

Start Laravel’s local development server to access your app in the browser.
php artisan serve
Visit: http://127.0.0.1:8000 to see your application and the Debugbar toolbar.


Fetch all users:
Update your route to retrieve all users from the database for testing.
<?php
use App\Models\User;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
    User::all();
    return view('welcome');
});
After updating the route, reload the browser and click on the Queries tab in Debugbar to view executed database queries.

This example shows how Debugbar captures all database queries executed by your application. Using User::all(), all users are fetched, and Debugbar logs the query details in real time. By exploring the Queries tab, you can analyze performance, spot slow or redundant queries, and make improvements such as using eager loading or indexing for better efficiency.

Step 8: Why Use Debugbar?

Laravel Debugbar helps you inspect and debug your application in real time. By default, it shows a few core tabs.
  1. Request: Overview of the current HTTP request.
  2. Timeline: Request duration, memory usage, and performance metrics.
  3. Views: Tracks rendered Blade templates and the data passed to them.
  4. Queries: Shows executed SQL queries and their execution time.

Additional tabs, such as Models, Exceptions, Mails, Session, Gate, and Queue, appear automatically only when your application uses the related features, ensuring you see relevant debugging information without clutter.

Conclusion

By completing this guide, you have successfully integrated Laravel Debugbar into your application, unlocking powerful debugging and performance monitoring capabilities. Debugbar now provides real-time insights into SQL queries, request performance, routes, view rendering, and exception logging, helping you efficiently optimize your Laravel application. With these tools, you can easily analyze database queries, pinpoint performance bottlenecks, and debug routing or view related issues. Going forward, you can further customize Debugbar, explore its additional features, and combine it with other debugging tools to streamline your development workflow.
For more details, please refer to the official Laravel Debugbar documentation.
Share this with friends!


"Give this post some love and slap that 💖 button as if it owes you money! 💸😄"
0

0 Comments

To engage in commentary, kindly proceed by logging in or registering