In this episode, we tackle a common problem: how do you profile API endpoints in Laravel, since tools like the Laravel DebugBar are mostly built for HTML responses? DebugBar adds all its info as HTML at the bottom of the page, so if you’re building APIs and serving JSON responses, that just isn't going to work (and would actually break your response).
To solve this, we build a custom middleware for our Laravel app that lets us append profiling/debugging information (like query details, response time, memory, and whatever else DebugBar tracks) straight onto our JSON API responses—but only when we want it. We show how you can trigger this by adding something like ?profile=1
to your endpoint, keeping debugging info out of normal responses. We also cover how you can configure what info to show (maybe just queries!) and how to easily switch it on and off.
Throughout the video, we go step-by-step starting from a fresh Laravel setup, getting DebugBar installed, registering our new middleware, and carefully ensuring that none of this leaks to production or non-debug environments. By the end, you'll have a neat little toggle to view full or partial profiling information with your API responses—super helpful for diagnosing queries and performance issues in your app.