title: How to pass data through a redirect to a view in laravel
body: <p>How can I pass data from a controller after it performs certain action to a view through a <code>redirect()</code> if I have a get route for it?</p>
  
  <p>The logic of the app is to redirect with an <code>user_id</code> to a view where the user will select its username after successfully verified its email.</p>
  
  <pre><code>public function confirm($confirmationCode){
   if(!$confirmationCode){
     dd('No se encontró ningún código de verificación en la URL');
   }
   $user = User::where('confirmation_code', $confirmationCode)-&gt;first();
   if(!$user){
     dd('Lo sentimos. Este código de confirmación ya ha sido usado.');
   }
   $user-&gt;confirmed = 1;
   $user-&gt;confirmation_code = null;
   $user_id = $user-&gt;user_id;
   $user-&gt;save();
  
   return redirect('assign-username')-&gt;with(compact('user_id'));
  }
  </code></pre>
  
  <p>The get route:</p>
  
  <pre><code>Route::get('assign-username', 'AuthenticationController@showAssignUsernameForm');
  </code></pre>
  
  <p>And the code for the post request of the <code>assign-user</code> form.</p>
  
  <pre><code>public function assignUsername(){
    $user_id = request()-&gt;input('user_id');
    $username = request()-&gt;input('username');
    if(User::where('username', '=', $username)-&gt;exists()){
      return redirect()-&gt;back()-&gt;withInput()-&gt;withErrors([
      'username' =&gt; 'Este usuario ya se encuentra registrado. Intenta nuevamente'
    ]);
    }else{
      DB::table('user')-&gt;where('user_id', $user_id)-&gt;update(['username' =&gt; $username]);
    }
   }
  </code></pre>
  
  <p>When trying to access to the <code>$user_id</code> variable it says it is not defined.</p>
  
  <p>The view's code:</p>
  
  <pre><code>@extends('layouts.master')
  @section('content')
      &lt;section class="hero"&gt;
          &lt;h1&gt;&lt;span&gt;Ya estás casi listo.&lt;/span&gt;Es hora de seleccionar tu nombre de usuario&lt;/h1&gt;
              &lt;div class="form-group"&gt;
                  &lt;form class="form-group"  method="post" action="assign-username"&gt;
                      {!! csrf_field() !!}
                      @if($errors-&gt;has('username'))
                          &lt;span class="help-block" style="color:red"&gt;
                              &lt;strong&gt;{{ $errors-&gt;first('username') }}&lt;/strong&gt;
                          &lt;/span&gt;
                      @endif
                      &lt;input type="hidden" name="user_id" value="{{ session('user_id') }}"&gt;
                      &lt;input type="text" name="username" placeholder="Escribe tu nombre de usuario"&gt;
                      &lt;button type="submit" class="btn" name="send"&gt;Registrar&lt;/button&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
          &lt;/section&gt;
  @endsection
  </code></pre>
  
  <p>Laravel Version: 5.2</p>