title: Laravel 5 - global exception handler for API requesting non-exist object
body: <p>I am using Laravel 5.3. I have few APIs where user will request for specific id. 
  For example url to subscribe an event<br>
  <code>example.com/api/event/{id}/subscribe</code></p>
  
  <p>Normally if <code>id</code> not exist, Laravel will return response 500 with error message "Trying to get property of non-object"</p>
  
  <p>So I would add check if event is exist like below in every controller where model 'id' id passed:</p>
  
  <pre><code>$event = Event::find($id)
  if ($event) {
      // return json data
  }
  else {
      return response()-&gt;json([
          'status' =&gt; 'object not found'
      ], 404);
  }
  </code></pre>
  
  <p>My question is, any better solution to handle this globally to check if object requested is not exist? My current solution is here, but I might think there should be better one</p>
  
  <p>I add this code into my <code>app/Exception/Handler.php</code>, so every api request non-existing object will return 404 with specific json message. So the API consumer will know the object id not valid.</p>
  
  <pre><code>public function render($request, Exception $exception)
  {
      // global exception handler if api request for non existing object id
      if ($request-&gt;wantsJson() &amp;&amp; $exception-&gt;getMessage() == 'Trying to get property of non-object') {
          return response()-&gt;json([
              'status' =&gt; 'object requested not found'
          ], 404);
      }
  
      return parent::render($request, $exception);
  }
  </code></pre>
  
  <p>Thanks in advance!</p>