Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.3 Change user table in Auth

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

like image 901
Adam Silva Avatar asked Dec 25 '22 01:12

Adam Silva


1 Answers

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!

like image 129
Koeffi Avatar answered Dec 28 '22 08:12

Koeffi