Search code examples
phplaravellaravel-artisan

Retrieve data from URL in laravel


I'm trying to make a referrals application. The only problem is that when I access the link that leads me to the register page, I have a parameter in the URL, I don't know, I don't realize how I can take that parameter from the URL and assign it to the created user. I tried to do a function but I don't realize, again, how I can get the data from that function variable in created user.

The column that generates referral links:

        <td>{{ route('refs.user',['name' => $referralCategory->name,'id'=>$referralCategory->id]) }}</td>

web.php

Route::get('refs/{name}/{id}','ReferralCategoryController@refs')->name('refs.user');

And the function that redirects to the register page

public function refs($name = null,$id = null){
        //dd($name);
        if(!$id){
            return redirect(route('login'));
        }
        else{

            if($id){
                $refCategory = ReferralCategory::where('id', $id)->first();
                ReferralCategory::where('id', $id)->update([
                    'referral_visits' => $refCategory->referral_visits + 1
                ]);
            }
            return redirect()->route('register', ['name' => $name]);

        }
        if(Cookie::get('id')){
            return(route('register'));
        }
        return response(route('register'))
        ->cookie('id', $id, 60*24*30*12)
        ->cookie('name',$name, 60*24*30*12);
    }

The register controller:

Route::get('register/{name}','Auth\RegisterController@register_ref')->name('register_ref.cat');
<?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 Illuminate\Http\Request;

    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',
                'email' => 'required|string|email|max:255|unique:users',
                'password' => 'required|string|min:6|confirmed',
                'referred_by' => 'string|max:20',
            ]);
        }

        public function register_ref( $name = null){


            return redirect()->route('register', ['name' => $name]);


        }

        /**
         * Create a new user instance after a valid registration.
         *


   * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {       


            return User::create(    [

                'name' => $data['name'],
                'email' => $data['email'],
                'password' => bcrypt($data['password']),
                'referred_by' => $data['referred_by'],

                ]);


    }


}

Solution

  • Variables, you pass through the route, can be called directly in the method:

    public function helloWorld($variableFromRoute) { }
    

    Better: If you want to register a user, you should work with http post request.

    <form method="post" action="{{ route('route_name') }}">
      <input type="text" name="username">
      <input type="password" name="pw">
      <input type="password" name="pw_repeated">
      <input type="submit" name="submit" value="Register">
    </form>
    
    public function register(Request $request) {
      // validate
      // create record
      // redirect
    }
    

    Best: ...Or just use the laravel auth system.