Home » Laravel » How to Debug Laravel Application

How to Debug Laravel Application

If you’re developing a complex and robust application, you may encounter unexpected behavior that requires, debugging to find the root cause. While traditional debugging techniques such as Xdebug, breakpoints, and step-by-step execution are helpful, they can be time-consuming and unnecessary in many cases.

One of the most popular ways to debug code is by printing the values of variables. Laravel provides two functions, dd() and dump() functions that allow you to print the value of any variable to the console or browser. However, there are also third-party packages available that can help you debug Laravel more efficiently. In this article, I will explain how to enable debugging in Laravel, how to use it’s built-in features, and how to simplify debugging with additional packages.

How to Enable Debugging in Laravel

Many debugging features in Laravel and third-party packages work only when debugging is enabled. You can enable it in the app/config.php file or using the .env file. Just make sure that the value of the APP_DEBUG parameter is set to true in the .env file:

APP_DEBUG=true

If you want to enable debugging for all environments by default, open the app/config.php file and ensure that the default value of the debug parameter is set to true:

return [ "debug" => (bool) env("APP_DEBUG", true), ];

How to Debug Laravel Application

1. dd() and dump() methods

The dump() helper allows to output the value of any variable to the browser or the standard output for command-line scripts. It uses the symfony/var-dumper package that displays almost everything in a human-readable and colorful way. For example:

dump("something"); dump(["one", "two", "three"]);

The dd() helper does the same thing but also aborts the application.

2. Debug Jobs in Laravel

Jobs in Laravel are executed in the background using the queue:work command. If you attempt to use dd() or dump() in the handle() method of these jobs, it will output the result into the output of the queue:work command. However, it may be more convenient to process your queued jobs synchronously during debugging. To do this, set the sync value to the QUEUE_CONNECTION parameter in the .env file:

QUEUE_CONNECTION=sync

In this case, you will get the result in the browser or command line command that dispatches the job.

3. laravel/telescope

The Telescope package is a powerful tool for debugging in Laravel. It provides information about requests, database queries, jobs, commands, events, and more. You can install this package using the following composer command:

composer require laravel/telescope --dev

For complete installation It requires running few additional commands, so refer to the official documentation for guidance.

One of the most commonly used features of Telescope is viewing information about requests. This feature is especially helpful when developing REST APIs. The tool allows you to check what data was received in each request and what was sent as a response. Additionally, you can find out which middleware and controller were used to handle the request and which database queries were executed. The Requests tab is the default location for opening this tool. Here, you can see a list of recent requests:

Clicking on each of them will provide you with more detailed information:

Information about database queries is located at the bottom of the page on the Queries tab:

Another useful feature of Telescope is recording dumps. By default, when you call the dump() helper, it outputs the value of the variable from the first parameter using echo. But if you open Telescope and navigate to the Dumps tab, it will display your dumps only there. For example:

Also, Telescope can catch emails that were sent from your application. You can find them on the Mails tab:

I have provided only the most interesting features of this package. You can check the official documentation to learn more about them and discover other useful tips.

4. barryvdh/laravel-debugbar

The laravel-debugbar package from barryvdh is designed for debugging pages that are generated using blade templates. It adds an informational panel at the bottom of the browser window, where you can find information about database queries, session info, used views, profiling, and more. You can install it using the following command:

composer require barryvdh/laravel-debugbar --dev

The package works only if debugging is enabled, so ensure that you have set APP_DEBUG to true in the .env file. The panel displays information only for the current page:

Of course, it has a history button on the right side of the panel, where you can view the requests history and open another request that was executed previously, but it is not as convenient for API queries as Telescope:

Once, the package is installed, you can use the Debugbar facade to print messages and variable values to the Messages table. It is more convenient than printing things directly in the content of the page. You can use these methods:

use Barryvdh\Debugbar\Facades\Debugbar; //.... Debugbar::info("Info message"); Debugbar::warning("Info message"); Debugbar::error("Error message"); Debugbar::error("Emergency message"); Debugbar::addMessage($object);

Also, all messages printed by the Log facade will appear on the Messages tab as well. Another nice feature of the package is measuring the time of execution. You can use the startMeasure() and stopMeasure() methods with an event name to measure the execution time of any part of your code. For example:

Debugbar::startMeasure("event"); sleep(10); Debugbar::stopMeasure("event");

All your measurements will be displayed on the Timeline tab:

Debugbar can also show you database queries. You can find all queries that were executed for the current page on the Queries tab:

Additionally, you can find detailed information about the current request, selected route, and middleware on the Route tab:

You can find additional information about this package on the official Github page.

5. LaraDumps Application

Maybe you have already heard about the spatie/ray package that allows to dump variables and other things into a standalone desktop application. However, it is a paid package. LaraDumps Application is a free and open-source alternative that allows to dump different variables in your app and view their values in a very convenient way in a desktop application. It can be very useful for local development, as you can print any variable, even from a queued job, and view it in beautifully formatted way. Additionally, this application allows to monitor SQL queries, Laravel logs, and debug LiveWire components.

You can download the application for your operating system here. Then install the package for your application using Composer:

composer require laradumps/laradumps --dev

After that, run the ds:init command to configure the package:

php artisan ds:init

This command will ask you a few questions about the LaraDumps features. You can choose which data will be dumped into the application. It can dump: SQL queries, external HTTP requests, jobs, commands, cache, logs, Liwewire components, and events.

You can change all these settings in the config/laradumps.php file or your .env file:

DS_APP_HOST=127.0.0.1 DS_APP_PORT=9191 DS_SEND_QUERIES=true DS_SEND_HTTP_CLIENT_REQUESTS=true DS_SEND_JOBS=true DS_SEND_COMMANDS=true DS_SEND_LOGS=true DS_SEND_LIVEWIRE_COMPONENTS=false DS_LIVEWIRE_EVENTS=false DS_LIVEWIRE_DISPATCH=false DS_SEND_LIVEWIRE_FAILED_VALIDATION=false DS_AUTO_CLEAR_ON_PAGE_RELOAD=false DS_AUTO_INVOKE_APP=false DS_PREFERRED_IDE=phpstorm

After this, you can use the ds() helper to print the value of any variable. For example:

$users = ["admin", "manager", "customer"]; ds($users);

If you have enabled dumping database queries, you can use this code to test that they are automatically dumped:

$users = User::all(); ds($users);

When your application outputs something into logs, you will see the Logs tab and all log messages:

You can find more examples of how to use this application in the official documentation.

Wrapping Up

In this short article, I have introduced several third-party packages that can be useful for debugging in Laravel. All of these packages are absolutely free and you can use them to make your development process faster. Which tools do you use for debugging? Share it, using the comments section below!

Leave a Comment