title: How to produce API error responses in Laravel 5.4?
body: <p>Whenever I make a call to <code>/api/v1/posts/1</code>, the call is forwarded to the <code>show</code> method</p>
  <pre class="lang-php prettyprint-override"><code>public function show(Post $post) {
      return $post;
  }
  </code></pre>
  <p>in <code>PostController.php</code> resourceful controller. If the post does exist, the server returns a JSON response. However, if the post does <strong>not</strong> exist, the server returns plain HTML, despite the request clearly expecting JSON in return. Here's a demonstration with Postman.</p>
  <p><a href="https://i.stack.imgur.com/ra049.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ra049.png" alt="enter image description here" /></a></p>
  <p>The problem is that an API is supposed to return <code>application/json</code>, not <code>text/html</code>. So, here are my questions:</p>
  <p><strong>1.</strong> Does Laravel have built-in support for <em>automatically</em> returning JSON if exceptions occur when we use implicit route model binding (like in <code>show</code> method above, when we have 404)?</p>
  <p><strong>2.</strong> If it does, how do I enable it? (by default, I get plain HTML, not JSON)</p>
  <p>If it doesn't what's the alternative to replicating the following across <em>every single</em> API controller</p>
  <pre class="lang-php prettyprint-override"><code>public function show($id) {
      $post = Post::find($id); // findOrFail() won't return JSON, only plain HTML
      if (!$post)
          return response()-&gt;json([ ... ], 404);
      return $post;
  }
  </code></pre>
  <p><strong>3.</strong> Is there a generic approach to use in <code>app\Exceptions\Handler</code>?</p>
  <p><strong>4.</strong> What does a standard error/exception response contain? I googled this but found many custom variations.</p>
  <p><strong>5.</strong> And why isn't JSON response still built into implicit route model binding? Why not simplify devs life and handle this lower-level fuss automatically?</p>
  <h3>EDIT</h3>
  <p>I am left with a conundrum after the folks at Laravel IRC advised me to leave the error responses alone, arguing that <em>standard HTTP exceptions are rendered as HTML by default</em>, and the system that consumes the API should handle 404s without looking at the body. I hope more people will join the discussion, and I wonder how you guys will respond.</p>