dongping4461 2017-07-19 00:29
浏览 238

使用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!

  • 写回答

3条回答 默认 最新

  • duanpo7282 2017-07-19 10:06
    关注

    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 ..

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测