3

I am begginer, and trying to create a referral system. following is sample url

http://dev.lea.com/register?ref=mh2HPLpVSn

I try to get referral string. how can i get separately? is there need of middleware for getting cookies? if yes then what will be code of middleware? and how can i get referral reference in RegisterController?

Here is my Register Controller.

<?php

namespace Lea\Http\Controllers\Auth;

use DB;
use Lea\User;
use Lea\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Cookie;

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 = '/';

    /**
     * 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, [
            'fname' => 'required|string|max:255',
            'lname' => 'required|string|max:255',
            'referral' => 'string|max:255',
            'username' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
            // 'tracking_id' => 'required',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \Lea\User
     */
    protected function create(array $data, Request $request)
    {

        //  printing here referral Link
        echo $request()->ref;


        // $referred_by = Cookie::get();
        // print_r($referred_by); 
        die;

        return User::create([
            'fname' => $data['fname'],
            'lname' => $data['lname'],
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'affiliate_id' => str_random(10),
            // 'referred_by' => $referred_by,
            'identity' => $data['identity'],
            'dob' => $data['dob'],
            'board_id' => $data['board_id'],
            'class_id' => $data['class_id'],
            'subject_id' => $data['subject_id'],
            'institute' => $data['institute'],
            'address' => $data['address'],
            'city' => $data['city'],
            'std_mobile' => $data['std_mobile'],
            'father_name' => $data['father_name'],
            'father_mobile' => $data['father_mob'],
            'ipadress' => \Request::ip(),
        ]);
    }

}
Hassan Raza
  • 1,454
  • 2
  • 13
  • 17
  • 1
    [this link](https://laravel.com/docs/5.5/requests) has everything you need. – Ali Nov 29 '17 at 10:37
  • When you have an URI such as /register?ref=mh2HPLpVSn, you can retrieve ref like this: `request()->ref` in your controller! Let me know if this works! – Hiren Gohel Nov 29 '17 at 10:37

4 Answers4

5

When you have an URI such as /register?ref=mh2HPLpVSn you can retrieve ref like this:

$request()->ref

in your controller! I believe you can also use $request()->has('ref') to determine if it's present in the URI.

Hope this helps you!!

Hiren Gohel
  • 4,942
  • 6
  • 29
  • 48
1

In laravel 6+ inside controller you can use like

$request->get('ResourcePath')
AbdulBasit
  • 1,269
  • 11
  • 19
0

You can get your parameters using Request access.
Here one example for get your ref code:

public function register(Request $request)
{
    $allParams = $request->all();
    $refParam = $request->ref;
    dd($allParams, $refParam);

    ...
}
Valkyrie00
  • 51
  • 1
  • 7
-1
public function showRegister(Request $request) {
    var_dump(request->ref)
}

then you can do things like validation and so on, another possiblity is to change the template of your register form and add a hidden field with the refcode from the ref param and handle the whole request in your register method.

Insax
  • 612
  • 4
  • 10
  • if i add Request $request then i got following error. Argument 2 passed to Lea\Http\Controllers\Auth\RegisterController::create() must be an instance of Illuminate\Http\Request, none given, called in E:\xampp\htdocs\laravel\lea\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 33 and defined – Hassan Raza Nov 29 '17 at 12:27