Custom User Table with Laravel 5.2

I got introduced to Laravel a few months back while searching for an MVC framework in PHP to build web applications. After playing around a bit with CodeIgniter and Laravel, I decided to proceed with Laravel.

I was blown away by the simplicity with which I can create my data model and seed the tables for initial development. It was quick and easy to pick and get going. It started getting a bit complex as I started using my own user class and tried to combine it with the Authentication model that comes with Laravel.

Given below is a quick summary of changes required if you need to use your own user table with the Authentication framework.

    • Follow laravel instructions to scaffold all routes and views needed for authentication
    • Update your model (default is User.php) to indicate your primary key.  Since I don’t use the default id, it looks something like this in my case

    • You may also want to add the following if password columns is also renamed.
    • Update config/app.php to tell Laravel, how users are retrieved. In my case, I use my_user_table instead of default User table.

    • Its bette that you leave the app/Http/routes.php as it is until you build rest of your business logic.
    • Add the following in app/Http/Controllers/Auth/AuthController.php. This tells the Eloquent model that we need to use username instead of email as the unique identifier for login

  • Update resources/views/auth/login.blade.php so that a variable called username is requested instead of email.

One caveat is about the error logging. Laravel does not have enough log messages to track down problems. Mostly exceptions thrown in the lower level are caught and handled internally which defeats the purpose of exceptions completely. I almost spent two days to figure out the variables needed as the login page kept coming back without any errors. It would have been much easier if there had been an error message before the exceptions were thrown.