title: Creating a Laravel 5.1 Middleware that can access my Model
body: <p>In Laravel 4.2 I had the following filter that prevented one user from viewing/editing/deleting a different user's course, which is an object based off of a "Course" model.  Here is the code I was using:  </p>
  
  <pre><code>Route::filter('restrictPermission', function($route)
  {
      $id = $route-&gt;parameter('id');
      $course = Course::find($id);
      $user_id = $course-&gt;user_id;
      if(Auth::user()-&gt;id !== $user_id)
          return Redirect::to('/')-&gt;with('flash_message', '*** Permission denied ***');
      # This compares the currently logged in user's id to the course's
      # user ID (in the database) so that the logged in user can 
      # only view or delete their own courses.
  });
  </code></pre>
  
  <p>Here is the Middleware I am trying to create that does the same thing as the above filter:</p>
  
  <pre><code>&lt;?php
  namespace App\Http\Middleware;
  use Closure;
  
  class RedirectIfWrongUser
  {
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @return mixed
       */
      public function handle($request, Closure $next)
      {
          $id = $route-&gt;parameter('id');
          $course = Course::find($id);
          $user_id = $course-&gt;user_id;
          if ($request-&gt;user()-&gt;id !== $user_id) {
              return Redirect::to('/')-&gt;with('flash_message', '*** Permission denied ***');
          }
  
          return $next($request);
      }
  }
  </code></pre>
  
  <p>The problem is that I don't know how to get the Middleware to recognize the Course class and the Course:: functionality.  </p>
  
  <p>Any constructive help would be most appreciated.</p>