2

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. :-)

1
  • Do you want to keep your users and admins separate in the database? Commented Sep 17, 2015 at 9:35

3 Answers 3

4

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',
     ],
],
Sign up to request clarification or add additional context in comments.

1 Comment

I define 'table' => 'my_user_table' with eloquent driver. I am under Laravel7 and there are also in RegisterController class a validator method with : 'email' => ['required', 'string', 'email', 'max:255', 'unique:my_user_table'], `
3

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

2 Comments

Thanks for the suggestion you gave me. I will try to use that and I will give you a feedback if it is ok. Thanks again. :-)
let me know if any query regarding this
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.