In this lesson, we will set one correct header value to avoid returning web pages instead of JSON results.
As you can see in the /api/categories/1, we see data for one category.

But what happens if we pass a missing category? We get an HTML page.

But for API, it is wrong. API expects the result as a JSON. In your client, set the headers to Accept -> application/json. Now Laravel knows to send the result as a JSON.

Another way is to set headers using Middleware. First, let's create a Middleware.
php artisan make:middleware AlwaysAcceptJson
Inside Middleware, we have a Request. Middleware is not just for preventing something; you can also add something...
It may be worth adding the reason why the reportable method needs to be changed by the renderable one. In Laravel the report method is for logging or reporting exceptions, while the render method is for customizing the response sent back to the user when an exception occurs. By separating these concerns, Laravel allows you to handle exceptions in a flexible and organized manner.
return response()->json(['message' => env('APP_DEBUG')? $e->getMessage() : 'Object not found'], 404);
Just use this
@donilearn Thanks for a good snippet.
I think this code is little better. It achieves accessing env from config and getting full error stack.