title: Building a Referral System with Laravel
body: <p>I've made a referral system on my laravel project v5.4 but I have 2 issues with that:</p>
  
  <ol>
  <li>My users referral link will load index page of my website instead of loading register page. (How I fix it?)</li>
  <li>When user register with referral link nothing will happen in database of the person who invited new user and also new user him/herself. (How I get info in both users tables?)</li>
  </ol>
  
  <p>I simply used this tutorial to get my referral system:
  <a href="https://brudtkuhl.com/building-referral-system-laravel/" rel="nofollow noreferrer">https://brudtkuhl.com/building-referral-system-laravel/</a></p>
  
  <p>This is my <code>CheckReferral</code> Middleware:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Middleware;
  use Illuminate\Http\Response;
  use Closure;
  
  class CheckReferral
  {
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @return mixed
       */
       public function handle($request, Closure $next)
       {
           if( $request-&gt;hasCookie('referral')) {
               return $next($request);
           }
           else {
               if( $request-&gt;query('ref') ) {
                   return redirect($request-&gt;fullUrl())-&gt;withCookie(cookie()-&gt;forever('referral', $request-&gt;query('ref')));
               }
           }
           return $next($request);
       }
  }
  </code></pre>
  
  <p>This is my <code>UserController</code></p>
  
  <pre><code>public function referral() {
        $user = User::find(1);
        return view('users.referral', compact('user'));
      }
  </code></pre>
  
  <p>Here is my route:</p>
  
  <pre><code>Route::get('/referral', 'UserController@referral')-&gt;name('referral');
  </code></pre>
  
  <p>My <code>RegisterController</code></p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers\Auth;
  
  use App\User;
  use App\Http\Controllers\Controller;
  use Illuminate\Support\Facades\Validator;
  use Illuminate\Foundation\Auth\RegistersUsers;
  use Cookie;
  use DB;
  
  class RegisterController extends Controller
  {
      /*
      |--------------------------------------------------------------------------
      | Register Controller
      |--------------------------------------------------------------------------
      |
      | This controller handles the registration of new users as well as their
      | validation and creation. By default this controller uses a trait to
      | provide this functionality without requiring any additional code.
      |
      */
  
      use RegistersUsers;
  
      /**
       * Where to redirect users after registration.
       *
       * @var string
       */
      protected $redirectTo = '/home';
  
      /**
       * Create a new controller instance.
       *
       * @return void
       */
      public function __construct()
      {
          $this-&gt;middleware('guest');
      }
  
      /**
       * Get a validator for an incoming registration request.
       *
       * @param  array  $data
       * @return \Illuminate\Contracts\Validation\Validator
       */
      protected function validator(array $data)
      {
          return Validator::make($data, [
              'name' =&gt; 'required|string|max:255',
              'username' =&gt; 'required|string|max:255|unique:users',
              'email' =&gt; 'required|string|email|max:255|unique:users',
              'password' =&gt; 'required|string|min:6|confirmed',
              'g-recaptcha-response' =&gt; 'required|captcha',
          ]);
      }
  
      /**
       * Create a new user instance after a valid registration.
       *
       * @param  array  $data
       * @return User
       */
      protected function create(array $data)
      {
  
          $referred_by = Cookie::get('referral');
  
          return User::create([
              'name' =&gt; $data['name'],
              'username' =&gt; $data['username'],
              'email' =&gt; $data['email'],
              'password' =&gt; bcrypt($data['password']),
              'affiliate_id' =&gt; str_random(10),
              'referred_by'   =&gt; $referred_by,
          ]);
      }
  </code></pre>
  
  <p><code>User</code> Model</p>
  
  <pre><code>protected $fillable = [
          'name', 'username', 'email', 'password',  'affiliate_id', 'referred_by',
      ];
  </code></pre>
  
  <p>And That's it!</p>