使用Laravel建立推荐系统

I've made a referral system on my laravel project v5.4 but I have 2 issues with that:

  1. My users referral link will load index page of my website instead of loading register page. (How I fix it?)
  2. 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?)

I simply used this tutorial to get my referral system: https://brudtkuhl.com/building-referral-system-laravel/

This is my CheckReferral Middleware:

<?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->hasCookie('referral')) {
             return $next($request);
         }
         else {
             if( $request->query('ref') ) {
                 return redirect($request->fullUrl())->withCookie(cookie()->forever('referral', $request->query('ref')));
             }
         }
         return $next($request);
     }
}

This is my UserController

public function referral() {
      $user = User::find(1);
      return view('users.referral', compact('user'));
    }

Here is my route:

Route::get('/referral', 'UserController@referral')->name('referral');

My RegisterController

<?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->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' => 'required|string|max:255',
            'username' => 'required|string|max:255|unique:users',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
            'g-recaptcha-response' => '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' => $data['name'],
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'affiliate_id' => str_random(10),
            'referred_by'   => $referred_by,
        ]);
    }

User Model

protected $fillable = [
        'name', 'username', 'email', 'password',  'affiliate_id', 'referred_by',
    ];

And That's it!

if this is for registration the right way to do this is first in your routes you should have a optional input like ..

Route::get('/register/{referral?},'Auth\RegisterController@registerPage');

then in that controller

public function registerPage($referral=0)
{
    return view with the $referral variable ..
}

in your view .. your form should look like this ..

<form action="/register/{{ referral }}" method="post" ..... >

back to your route ..

Route::post('/register/{referral},'Auth\RegisterController@doRegister');

in your controller again ..

public function doRegister(Request $request, $referral)
{
    $request->merge(['referred_by' => $referral]);
}

so your referred_by is either 0 or other value .. it's up to you how you handle the validation ..

Add register page url in the value in this part -> url('/')

@if(!Auth::user()->affiliate_id)
        <input type="text" readonly="readonly" 
               value="{{url('/').'/?ref='.Auth::user()->affiliate_id}}">
    @endif

Question number 1:

I think you should add {{url('/register')}} here instead of {{url('/')}} to make it look this way:

@if(!Auth::user()->affiliate_id)
        <input type="text" readonly="readonly" 
               value="{{url('/register').'/?ref='.Auth::user()->affiliate_id}}">
    @endif

If that's how your register route endpoint is defined.