Jerielle
Jerielle

Reputation: 7520

How to use laravel's Auth class in different table?

I created a simple login and registration in my page and I also added some new columns in the default users table. Now my problem is I have another table named as admin and the default Laravel's Auth table is users. How can I implement the same functionality in my table?

In the Users model it has the declaration for the table name

   protected $table = 'users';

Can you give me an example how can I use the default laravel's auth class?

Thats all thanks. :-)

Upvotes: 2

Views: 16282

Answers (3)

Bhaskar Bhatt
Bhaskar Bhatt

Reputation: 1467

Laravel takes default users table for an application. For a change of laravel authentication different table relevant table name, we need to make a small change in authentication file of config.

Go to

config/auth.php

'providers' => [
    // 'users' => [
    //     'driver' => 'eloquent',
    //     'model' => App\User::class,
    // ],
     'users' => [
         'driver' => 'database',
         'table' => 'user',
     ],
],

Upvotes: 4

CIZO
CIZO

Reputation: 4167

Do you hear about Multiauth in laravel. in this library there are two or more type user can login in one laravel application. In our case there are two type user Admin and Public that means User right.

Both forgot password and reset password functionality works separately in one application.

After install this library have have one step like below.

'multi' => [ 'admin' => [ 'driver' => 'database', 'table' => 'admin', 'email' => 'client.emails.password' ], 'users' => [ 'driver' => 'database', 'table' => 'users', 'email' => 'client.emails.password', ] ],

change your Auth.php file code with this one.

installation

Firstly you want to include this package in your composer.json file.

 "require": {
        "sboo/multiauth" : "4.0.*"
}

Now you'll want to update or install via composer.

composer update

Usage

Everything is done the exact same way as the original library, the one exception being that all method calls are prefixed with the key (account or user in the above examples) as a method itself.

Auth::admin()->attempt(array(
    'email'     => $attributes['email'],
    'password'  => $attributes['password'],
));
Auth::client()->attempt(array(
    'email'     => $attributes['email'],
    'password'  => $attributes['password'],
));
Auth::admin()->check();
Auth::client()->check();

Here is your library

Upvotes: 3

Javier Núñez
Javier Núñez

Reputation: 622

I don't think the best way is to duplicate your table. I would extend users table with a role field that indicates if the user is a standard one or an admin. This way, you can keep the same code and add the ADMIN functionality that you are looking for.

If you NEED to do that and you are using Laravel 4, maybe you can use this plugin:

https://github.com/ollieread/multiauth/

Also in this thread you have code that implements Auth in different tables:

https://gist.github.com/danielcoimbra/64b779b4d9e522bc3373

But I strongly suggest to integrate both tables in one with an Admin flag/field

Upvotes: 0

Related Questions