LasseLaursen.com


Changing standard field/column names in laravel 5's default users table

Monday July 6, 2015
by Lasse Laursen

A while back I wondered about the best practices in regards to database naming conventions. In my search I happened to come across an article by Robert Pittenger. I read it and found it made some sense to me, enough so that I've adopted a number of the naming conventions he introduces. Having recently rebuilt this website using Laravel I - like a few others - ran into some issues with trying to change the standard field/column names in the 'users' table. So let's look at what needs to be done.

Note: I assume that the default authentication driver you use is eloquent and not database, as requires a few different changes.

For the sake of full disclosure, a few vocal supporters would argue that this is a silly endeavor and that venturing forth with these changes is going against the whole purpose of using a pre-existing framework. Personally, I do not agree with this argument, and think that when it comes to something like database names the framework should absolutely yield and allow this to be as customizable as possible. Being able to change this makes the framework better. Finally, this is of course only necessary if you - like me - are lazy and would like to make use of Laravels integrated authentication code. If you're building that from scratch, you really needn't bother with any of this. Anyway - I digress; we'd like to change the following 5 things:

  • The table name from 'users' to 'tblUser'
  • The 'id' field to 'useID'
  • The 'username' field to 'useName'
  • The 'email' field to 'useEmail'
  • The 'password' field to 'usePassword'

You may have noticed I don't bother changing the timestamp fields. That's because my OCD only takes me so far.

Changing the table name is a cinch. Simply open the provided /app/User.php model file and change the associated table name thusly:

	//protected $table = 'users';
	protected $table = 'tblUser';

Chaning the 'id' field is also fairly straight forward. We simply need to override the $primaryKey member that the class inherits as such:

    protected $primaryKey = 'useID';

Easy.

Changing the 'username' field doesn't require any re-coding (except for one part I note at the end), so just rename that field.

Now things get a little trickier, as both the 'email' and 'password' fields are a little more deeply integrated into Laravels existing authentication code. But not so much that a few quick overrides can't help us out. Looking at the provided login view (located at /resources/views/auth/login.blade.php) we obviously need to change the name of the input field for 'email', to 'useEmail'. Naturally one would also want to change the 'password' field to 'usePassword', but hold off on that for now.

If we use the page, we'll run into some validation errors as the existing back-end code still expects a filled field entitled 'email'. If you open up the /app/Http/Controllers/Auth/AuthController.php file, nothing immediately stands out as problematic, but that's because all the heavy lifting is actually handled by the 'AuthenticatesAndRegistersUsers' class. The 'postLogin' function is the problem, but since editing vendor files will cause all kinds of headaches (when you eventually update), make a copy of it and place it in the 'AuthController' class thus overriding the inherited functionality. With a few changes we'll now be referencing the altered field name:

    // Override to use 'useEmail'
    public function postLogin(Request $request)
    {
        $this->validate($request, [
            'useEmail' => 'required|email', 'password' => 'required',
        ]);

        $credentials = $request->only('useEmail', 'password');

        if ($this->auth->attempt($credentials, $request->has('remember')))
        {
            return redirect()->intended($this->redirectPath());
        }

        return redirect($this->loginPath())
            ->withInput($request->only('useEmail', 'remember'))
            ->withErrors([
                'useEmail' => $this->getFailedLoginMessage(),
            ]);
    }

Four down, one to go. You'll notice the 'password' field name has remained unchanged. Well, that's because it's more integrated than 'email' field was, and there's a very simple surface-level fix that'll take care of everything. Back to the /app/User.php file, and add the following override:

    // Override required, otherwise existing Authentication system will not match credentials
    public function getAuthPassword()
    {
        return $this->usePassword;
    }

This will now provide Laravel with the proper password credentials stemming from our renamed field.

Finally, make sure to also update the $fillable and $hidden arrays with the new field names and you should be good to go!

Fields named however you want them and Laravels useful authentication remains in-tact!


Filed under: