Laravel 5.3 Change user table in Auth

2019-02-15 00:36发布

I'm using Laravel 5.3 and used the make:auth artisan command to scaffold the login/registration system. I'm doing my login as companies, so I have a table called Company. How do I change the original sql to go get the email and password from the Company table instead of the User table?

I already tried to change in the config/auth.php file in the providers part, but when I changed 'model' => App\User::class, to 'model' => App\Company::class,, it started logging in, but regardless if the email and password input were completely wrong. Any ideas?

EDIT: After the Company registers and logs in, it has the ability to invite Users, therefore the original User table has to remain

2条回答
Ridiculous、
2楼-- · 2019-02-15 00:40

DO NOT FORGET TO CHANGE VALIDATION IN REGISTERCONTOLLER.PHP AS WELL.

from

'email' => 'required|email|max:255|unique:users',

to

'email' => 'required|email|max:255|unique:company',
查看更多
Emotional °昔
3楼-- · 2019-02-15 00:57

Laravel 5.3 has changes in the Auth implementation. For me, this way solved it:

First, provide a company table in the database that fulfils the criteria to be used for identification. Thus, it needs a name, email, password and remember_token column. Details can be found here.

In the config/auth.php change the users model to your company class.

    'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Company::class,
    ],

Create a Company class in the App folder that extends the Auth, so use:

use Illuminate\Foundation\Auth\User as Authenticatable;

In the Company class, define fillable and hidden fields.

class Company extends Authenticatable {

protected $fillable = [
    'name', 'email', 'password',
];

protected $hidden = [
    'password', 'remember_token',
];
}

In the RegisterController.php change "use App\User" to

use App\Company;

Adjust the create and validator function in the RegisterController.php with Company::create

protected function create(array $data)
{
    return Company::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:companies',
        'password' => 'required|min:6|confirmed',
    ]);
}

'email' => 'required|email|max:255|unique:companies'
(table name for Company Model will be companies)

Hope this helps!

查看更多
登录 后发表回答