I’m not far from the truth, if I say, debugging can take as much time as planning or coding a feature. So let’s see some commonly used debugging techniques in Laravel. From small to large scale.
Die and dump
The “Die and dump” is one of the most used features of Laravel (it was built on the top of the Symfony var dumper package). Using the function will print out the given parameter and also exit the code. It means you can use it as a breakpoint in your application while inspecting a variable or something else.
// dumps the variable out and stops the code running as well dd($toInspect);
Using the tinker console
Another awesome tool is the tinker, which comes with Laravel by default. When we need to test only one thing – like a model or a small service, maybe a helper – it’s nice to just run the php artisan tinker command and run the code in the console. We see the results quicker and no need to modify our real code.
Logging can be extremely useful. For example, if we need to find out what’s happening in an AJAX request, mostly dd() is not working properly. But we can log the data, and inspect the result at the log file.
Let’s move from the small in-code solutions. The debugbar is not a manual solution like the previous ones. With this package, you can easily track your application. You can inspect queries, messages, requests, routes, views and so on. It can be a very handy company when you are working with a larger application.
Let’s jump a bigger one. Laravel Telescope is a first-party package that helps you monitoring your application activities. It can be extremely useful, especially if your application contains some features that are not easy to track down with simpler solutions like above. This package was made for a complete application monitoring, so we can look at it like it’s an extended debugbar.
Telescope comes with its own surface, so you don’t even need to customize or create any extra integration step. Laracasts has a nice introduction video to Telescope, it’s highly suggested to give it a try.
Need a web developer? Maybe we can help, get in touch!