title: Middleware for checking if resource is owned by user
body: <p>I'm having some trouble making middleware that checks if the user owns the resource being requested.</p>
  
  <p>For example, if the user goes to <code>/playlists/1/edit</code>, and they do not own playlist 1, it should display a 401 error.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>class CheckOwnership {
  
      public function handle(Request $request, Closure $next)
      {
          if (Playlist::find($request-&gt;route()-&gt;parameters()['playlists'])-&gt;user_id !== $request-&gt;user()-&gt;id)
          {
              return response('Unauthorized.', 401);
          }
  
          return $next($request);
      }
  
  }
  </code></pre>
  
  <p>This is terrible and only works for the Playlist resource, but I can't find any better way of doing this.</p>
  
  <p>Thanks</p>