2

How can i change the default user model to use other database table instead of users table in laravel 5.3, is adding

protected $table = 'my_table_name';

will do everything or do i have to change the providers and all.

I dont wish to use multiple authentication like admin, customers etc i just want one authentication model but not with the table "users".

I am preparing an application which has multiple type of users like admin, astrologers and users. all of them have different attributes so i cant use single model with access control in it so i have decided to divide the application into 3 websites to use single database and host them in subdomains like

admin.mywebsite.com
astrologer.mywebsite.com
www.mywebsite.com

earlier i was using multiauth/hesto plugin but for some strange reason it's not redirecting me to the intended url after login and registration. any help will be appreciated.

Taj Khan
  • 490
  • 6
  • 19

4 Answers4

6

check this one Using Different Table for Auth in Laravel

Edit app/config/auth.php to change the table.

'table' => 'yourTable'
'model' => 'YourModel',

then your model:

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class YourModel extends Eloquent implements UserInterface, RemindableInterface 
{
    use UserTrait;
    use RemindableTrait;

    protected $table = 'your_table';
    protected $fillable = array('UserName', 'Password');    
    public $timestamps = true;
    public static $rules = array();    
}
Kiran Maniya
  • 5,637
  • 4
  • 34
  • 53
ashanrupasinghe
  • 998
  • 1
  • 9
  • 21
1

You can specify which database to use using the $connection attribute in your model:

class MyModel extends Eloquent
{
    protected $connection = 'another-database-connection';
}

I'm not sure it's what you're looking for tho. That said, if you want to use the same Laravel application for multiple subdomains, I'd recommend checking the documentation which explains how to use subdomains in your routes:

https://laravel.com/docs/5.3/routing#route-group-sub-domain-routing

This would allow you to have a single User class and a single application but have specific routes for each of your 3 subdomains.

AntoineB
  • 4,010
  • 3
  • 23
  • 53
  • i want to use the same connection for all three subdomains, only thing i want is to change the User model to use "astrologers" table instead of "user" table and "admin" table for admin subdomain – Taj Khan Dec 07 '16 at 10:39
  • Then I believe you have to have a `User` model and then an `Astrologer` and an `Admin` one that will both have a `belongsTo` relationship with the `User` model. – AntoineB Dec 07 '16 at 10:54
1

go to your config\auth.php

modify the configuration

'guards' => [
    'astrologer' =>[
        'driver' => 'session',
        'provider' => 'astrologer',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admin',
    ],
],  

'providers' => [
    'astrologer' => [
        'driver' => 'eloquent',
        'model' => App\Astrologer::class,
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],

See answer here for more details: Can anyone explain Laravel 5.2 Multi Auth with example

Community
  • 1
  • 1
aimme
  • 5,067
  • 4
  • 42
  • 54
  • the answer is right but i am using laravel 5.3 which is very different i tried but with no success :( – Taj Khan Dec 07 '16 at 11:22
  • @TajKhan any luck? could you please post what you did. remember to implement the auth interfaces. – aimme Dec 08 '16 at 10:04
0

Go to config/auth.php Change the provider for this case I changed the api provider from user to custom_user and then use custom_user model class

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api' => [
        'driver'=>'passport',
        'provider'=>'custom_user'
    ],
]



'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'custom_user' => [
        'driver' => 'eloquent',
        'model' => App\custom_user::class,
    ],
]
Aman
  • 1,323
  • 2
  • 12
  • 19