I'm new to Laravel. I don't know much about how The RegisterController works. I mean I can add some new fields or delete some from the Register form. But now I want to something else.

I want to add another field in the Register Form called "Code". I have a "codes" table in database with Ten codes in it. If the user's value in Code field matches any code from the "codes" table, only then the new user will be able to register and view the contents of the website. How can I implement this? Any help will be greatly appreciated ...

    
if no user has to be able to manage the codes, I suggest you place an array in a file in your config folder. You can access config files like this: Config::get('yourFile.arrayKey');. In the (php)file, return an anonymous array: return [ 'yourKey' => array('code1', 'code2') ];. You can read more here scotch.io/tutorials/how-to-use-laravel-config-files. Less db selects =p – Maarten Kuijper 1 hour ago

2 Answers 2

The register controller uses a trait called RegistersUsers.

You need to "override" the register method in your controller:

class RegisterController extends Controller {
       use RegistersUsers { register as protected traitRegister; }

      public function register(Request $request) {
            //Assume a table "codes" and a field "code" in your register form  
            if (DB::table("codes")->where("code","=",$request->code)->count() > 0) {
                $this->traitRegister($request); //Call the normal registration
            } else {
                abort(401,"Unathorised");
            }
      }


}  
    
I get this: ReflectionException in Route.php line 339: Class App\Http\Controllers\Auth\Request does not exist – Saroar 1 hour ago
    
Add use Illuminate\Http\Request; on the top of the file after the namespace declaration. Actually copy all the use directives from outside the linked trait to be safe. – apokryfos 1 hour ago

in your register form add an input called codes with other fields

in controller

public function register(){
$codes = Input::get('codes');
$exist = DB::table('codes')->where('codes','=',$codes);
if ($exist){
// take email password and register
}
else{
// redirect with error message
}

}