1

I'm finding a way to authenticate my users in Laravel application. However, all I want is that I can configure the table name, and specify the field for logging in (as I want to use Username and Password).

Aside from changing the table name and fields in the migration file, are there anything I have to do next?

srakrn
  • 352
  • 2
  • 16
  • So you want to change the table name and use username field for login thats all? i can help you but without the sentiel package – Achraf Khouadja Jun 17 '16 at 03:08
  • Yes, answer to my previous question about Laravel auth mentions the use of Sentinel, so I think it's an easier way. But after trying, seems the docs isn't really clear. If possible, please help me on changing table name, username field, and password field please, thanks! – srakrn Jun 17 '16 at 03:34
  • the answer works 100% – Achraf Khouadja Jun 18 '16 at 03:46

1 Answers1

0

Use php artisan make:auth and then :

To change the default table from users which uses the User model you have to navigate to : config/auth.php

And make these changes

 'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\UsersTable::class, //put the right model name here or you can use database driver
        ],
    ],

To change the table for an eloquent model you can do this

use Illuminate\Database\Eloquent\Model;

    class UsersTable extends Model
    {
        /**
         * The table associated with the model.
         *
         * @var string
         */
        protected $table = 'UsersTable';
    }

To change the default login credential from email to user name , just add this to the Authcontroller generated by laravel

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $redirectTo = '/home';
    protected $username = 'username';  // you can put whatever column you want here from your table

Dont forget to change the validation stuff

EDIT dont forget to add the username to the model like so

protected $fillable = [
    'name', 'username', 'email', 'password',
];
Achraf Khouadja
  • 6,119
  • 4
  • 27
  • 39