Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

HarryMcKinney's avatar

Add Variable to Auth Registration by the URL

Through the lessons I was able to modify a url easily with a variable and pass it through the routes. I ran into trouble with the Auth Registration routes though. Here is what I have tried to do:

The URL I am trying to pass is: https://myapp.dev/register/101

to add the '101' to the user's record (it's a specific group identifier) I have tried this:

NOTE: I have already added a table to support the ID and I have added a hidden field in the registration to save it to the form. I am just trying to get Laravel to support the route at this point.

I removed Auth::routes(); and I added:

// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...
Route::get('register/{group_id}', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');

// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');

and I added this to the app/http/auth/registercontroller.php

public function showRegistrationForm($group_id)
{
    return view('auth.register', compact('group_id'));
}

In the web.php I have tried this also:

Route::get('register/{group_id}', function ($group_id) {

    return view('Auth\RegisterController@showRegistrationForm')->name('register', compact($group_id));

});

instead of:

Route::get('register/{group_id}', 'Auth\RegisterController@showRegistrationForm')->name('register');

I've also tried differing forms of the route with things like:

Route::get('register/{group_id}', 'Auth\RegisterController@showRegistrationForm', $group_id)->name('register');

and several with->('group_id') and even withData along with any other advice I have received over the past few days.

The errors I am seeing are: View [Auth\RegisterController@showRegistrationForm] not found. group_id is undefined and Missing required parameters for [Route: register] [URI: register/{group_id}]. (View: /Applications/MAMP/htdocs/makeauth/resources/views/auth/register.blade.php)

any help and advice is heavily appreciated at this point! Thanks in Advance!

0 likes
16 replies
Snapey's avatar

Using Laravel 5.5

web.php;

Route::get('register/{group_id}', 'Auth\RegisterController@showRegistrationForm')->name('register');

RegisterController.php

    public function showRegistrationForm ($group_id)
    {
        dd($group_id);
    }

and then hit the route /register/101

results in "101" being displayed in the browser as expected.

Can you reproduce as this and report your outcome?

HarryMcKinney's avatar

Snapey-> Thank you for your help! When I add the dd I get the expected JSON response of '101', my question is in overriding the function in the registerController.php file, in following the convention of the file in the vendor folder wouldn't I do this?:

public function showRegistrationForm($group_id)
    {
        //dd($group_id);
        return view('auth.register');
    }

but returning the view like this or even when adding:

public function showRegistrationForm($group_id)
    {
        //dd($group_id);
        return view('auth.register', compact('group_id));
    }

I am getting this error: Missing required parameters for [Route: register] [URI: register/{group_id}]. (View: /Applications/MAMP/htdocs/makeauth/resources/views/auth/register.blade.php) I feel like I am missing something obvious or simple in the function? Any suggestions? Thanks again.

RamjithAp's avatar

I guess your are not passing required parameter or missing something, if you can show us your current code for register link, route and controller we can help you.

1 like
robrogers3's avatar

What do you see when you run

$php artisan route:list make sure yours shows up correctly.

Then check the http request in the Dev tools in your browser. Make sure that's calling the correct method. With to group id.

Then check your access log.

Also dig deep and hack the Routes class in the laravel vendor folder. And comment out the auth method and see what happens.

Finally and perhaps this should be first remove the parameter requirement to your route and make sure it gets called And then check the $server output for the path.

Let us know what you find

1 like
HarryMcKinney's avatar

RamjithAp => I'm working in a fresh install of Laravel, I am adding this to a large project but I was afraid something else in the project was stopping the code from working so I started fresh to diagnosis this. The only things I have altered after make:auth are the web.php file and the RegisterController.php file.

robrogers3 => This is my routes:list

+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| Domain | Method   | URI                    | Name             | Action                                                                 | Middleware   |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
|        | GET|HEAD | /                      |                  | Closure                                                                | web          |
|        | GET|HEAD | api/user               |                  | Closure                                                                | api,auth:api |
|        | GET|HEAD | home                   | home             | App\Http\Controllers\HomeController@index                              | web,auth     |
|        | GET|HEAD | login                  | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest    |
|        | POST     | login                  |                  | App\Http\Controllers\Auth\LoginController@login                        | web,guest    |
|        | POST     | logout                 | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web          |
|        | POST     | password/email         | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest    |
|        | GET|HEAD | password/reset         | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest    |
|        | POST     | password/reset         |                  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest    |
|        | GET|HEAD | password/reset/{token} | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest    |
|        | POST     | register               |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,guest    |
|        | GET|HEAD | register/{group_id}    | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest    |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+

I'm thinking that only the GET route needs to have the group_id and not the POST do you agree with that? Also in listing the routes I noticed the {token} on the route so I am following that path to see if there is anything I have missed as well.

Any thoughts?

HarryMcKinney's avatar

Following the same convention of the password/reset/{token} with my register/{group_id} I rewrote the RegisterController.php file like this:

public function showRegistrationForm($group_id)
    {
        //dd($group_id);
        return view('auth.register')->with(['group_id' => $group_id]);
    }

but that brought me back to this error:

Missing required parameters for [Route: register] [URI: register/{group_id}]. (View: /Applications/MAMP/htdocs/myapp/resources/views/auth/register.blade.php)

I really think there is some simple syntax that I am overlooking or some additional place this $group_id variable should be listed, thanks in advance for any help or assistance -> any thoughts?

Snapey's avatar

Show your registration form. The error implies that your form action is expecting the group_id

1 like
HarryMcKinney's avatar

Sure, here is the error I am receiving:

Missing required parameters for [Route: register] [URI: register/{group_id}]. (View: /Applications/MAMP/htdocs/makeauth/resources/views/auth/register.blade.php)

Here is my RegisterController.php file:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;


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;

    public function showRegistrationForm($group_id)
    {
        //dd($group_id);
        return view('auth.register')->with(['group_id' => $group_id]);
    }


    /**
     * 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',
            'group_id' => 'required|integer'
        ]);
    }

    /**
     * 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']),
            'group_id' => $data['group_id']
        ]);
    }
}

Here is the register.blade.php view:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Register</div>

                <div class="panel-body">
                    <form class="form-horizontal" method="POST" action="{{ route('register') }}">
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <label for="name" class="col-md-4 control-label">Name</label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>

                                @if ($errors->has('name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>

                                @if ($errors->has('email'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label for="password" class="col-md-4 control-label">Password</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control" name="password" required>

                                @if ($errors->has('password'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>

                            <div class="col-md-6">
                                <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
                            </div>
                        </div>

                        <input id="group_id" type="hidden" class="form-control" name="group_id" value="{{{ $group_id }}}" required>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    Register
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

The group_id variable is introduced at line #64. Here is my users table file:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('group_id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

I have run a migration on it and I can confirm the table along with the column is in the database. Here is my web.php file:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

//Auth::routes();

// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...
Route::get('register/{group_id}', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');

// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');

Route::get('/home', 'HomeController@index')->name('home');

Any help, assistance or suggestions are greatly appreciated, this is all I have been working on for the past few days

Snapey's avatar

its because this

Route::post('register', 'Auth\RegisterController@register');

does not have a name, but in the form you are saying action is route('register') and route register is your get route which requires the group_id

change this

Route::post('register', 'Auth\RegisterController@register')->name('post.register');

and set the form action

action={{ route('post.register') }}
HarryMcKinney's avatar

Thanks, I have made the changes and this is the error I am seeing:

Missing required parameters for [Route: register] [URI: register/{group_id}]. (View: /Applications/MAMP/htdocs/makeauth/resources/views/layouts/app.blade.php) (View: /Applications/MAMP/htdocs/makeauth/resources/views/layouts/app.blade.php)

I've also tried adding /{group_id} to the post route, and I have tried overriding the Auth\RegisterController@register function by adding the $group_id variable next to request (with a ,). I've started with a fresh install of Laravel with the latest release 5.5.24. The dd call works perfectly within the function, it's just when I remove it and rely on the view that I receive the error. Any thoughts?

robrogers3's avatar

Remember

LOG::info is your friend here. Way better than dd when your digging see.

1 like
Snapey's avatar

In addition to what I mentioned before, does your default layout include a link to the registration page?

Anywhere route('register') is mentioned is not going to work without modification because you want that extra parameter for the group_id

The alternative is to make group_id optional and then check if it has been provided in the controller.

1 like
robrogers3's avatar

try this

<form action="{!! route('register', ['groupid' => $groupid]) !!}">
    html here
</form>

the array after the route name, is the parameters your missing.

2 likes
HarryMcKinney's avatar

Got it! Here's How:

(1) In the migrations folder I added the variable to the table:

$table->integer('group_id');

(2) In the model User.php I added the variable to the fillable array:

protected $fillable = [
        'name', 'email', 'password','group_id'
    ];

(3) In web.php I removed Auth::routes(); and added:

// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...
Route::get('register/{group_id}', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register')->name('post.register');

// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');

(4) In RegisterController.php I added a function to override the function in the vendor folder:

public function showRegistrationForm($group_id)
    {
        //dd($group_id);
        return view('auth.register')->with(['group_id' => $group_id]);
    }

Then added the variable to the create function:

protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'group_id' => $data['group_id']
        ]);
    }

(5) In register.blade.php I added a hidden input field:

 <input id="group_id" type="hidden" class="form-control" name="group_id" value="{{{ $group_id }}}" required>

and thanks to @Snapey I changed the route to:

action="{{ route('post.register') }}"

and also Thanks to @Snapey I changed the in app.blade.php to:

action="{{ route('post.register') }}"

Now WHY did I do it? I want to give users a unique registration link and then tailor their experience by the unique number in the URL.

My last question is: "was there a better way to do this?"

Thanks so much to @robrogers3 , @Snapey @RamjithAp and to anyone else who took the time to read this!

Snapey's avatar
Snapey
Best Answer
Level 122

My last question is: "was there a better way to do this?"

probably not.. not if you wanted it to function off the route and not by asking the user to key in the groupID

1 like

Please or to participate in this conversation.