title: Create middleware to prevent a user manipulates the information of other users in laravel 5.1
body: <p>I have a problem.
  I need to prevent a logged in user can edit / view etc. information of another user changing the id in the url.</p>
  
  <p>For example:</p>
  
  <p>The URL is something like <strong>myapp/users/32/edit</strong>, but to change the id, can edit the information of other users</p>
  
  <p>I tried to create a middleware, but not achieve expected results.
  I created the following:</p>
  
  <p>L</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Middleware;
  
  use Closure;
  use Auth;
  
  class UserSecutiry
  {
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @return mixed
       */
      public function handle($request, Closure $next)
      {
  
          if ($request-&gt;user()-&gt;id == Auth::id()) {
              return $next($request);
          }
  
          return redirect()-&gt;to('/');
      }
  }
  </code></pre>
  
  <p>But <strong>$request->user->id</strong> returns me logged user id, so the middleware does not work and can manipulate any user information changing the id.</p>
  
  <p>How I can accomplish this task. I hope your help. Greetings from Chile</p>