title: Check if model belongs to user for specific routes - Laravel 5.1
body: <p>I want to check if a recipe belongs to the logged in user. The best way is middleware I guess. </p>
  
  <p>So I created a middleware: AuthRecipe, containing the following code:</p>
  
  <pre><code> $repo = new RecipeRepository;
   $recipe = $repo-&gt;getById($request-&gt;recipes);
  
   if($recipe-&gt;user_id !== $request-&gt;user()-&gt;id)
   {
      return redirect()-&gt;to('/');
   }
  </code></pre>
  
  <p>In the RecipeController I use the middleware like:</p>
  
  <pre><code>$this-&gt;middleware('auth.recipe', ['only' =&gt; ['update', 'edit', 'destroy']]);
  </code></pre>
  
  <p>This works fine, if the recipe doesn't belong to the user, the user gets redirected to home, but...</p>
  
  <p>Now I have duplicated code. If the recipe belongs to the user, it will get the recipe inside the middleware, but also in the controller itself. So I already know the recipe and don't need to get the recipe again in the controller.</p>
  
  <p><a href="https://i.stack.imgur.com/5gTkI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5gTkI.png" alt="enter image description here"></a></p>
  
  <p>Of course I can use a method in the model itself.</p>
  
  <pre><code>public function auth() {
      if($this-&gt;user_id !== Auth::user()-&gt;id) {
          redirect()-&gt;to('/');
      }
  }
  </code></pre>
  
  <p>But then I have to call that method for every method that needs to be protected. I think the middleware is more cleaner. Calling <code>$recipe-&gt;auth()</code> is not really a controller's responsibility. </p>
  
  <p>Is there some way to pass the recipe data from the middleware to the controller?</p>