Question: Laravel 5.3 Multi Authentication setup

Question

Laravel 5.3 Multi Authentication setup

Answers 3
Added at 2017-01-03 10:01
Tags
Question

I would like to use laravel 5.3 authentication for two different tables. How would it be possible by using laravel authentication?

I have setup auth.php files, and adding guards, Creating a new model, and migrate the necessary file.

I copied the controllers, and update the namespace, and have specified the route. And, yes i am able to access that route, to that particular controller..

But, when i register, it goes to users table. Why and how to set it to the correct table? I thought guards would do the work.

And also, If we use the default laravel authentication , we are not able to set the route? I mean, i am able to access it, and change it on the route, but when the register post, it goes back to the default route. I wonder if there's any way to override that..

Thanks a lot

Answers to

Laravel 5.3 Multi Authentication setup

nr: #1 dodano: 2017-01-03 10:01

What do you mean by the correct table.You should edit the table name you requied in the register controller Ex:-

$company=  Company::create([
             'companyname' =>($data['companyname']),
             'Address1' =>($data['Address1']),
             'Address2' =>($data['Address2']),
             'telephone' =>($data['telephone']),

        ]);
nr: #2 dodano: 2017-01-03 10:01

I am also new but Try to change the table in Register controller

protected function create(array $data)
{

    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'user_type'=>$data['user_type'],

    ]);

Replace "User" to what you want to enter the data..

As i understood the auth.php... in there we use the model(table ) to login..

'providers' => [
    'users' => [

        'driver' => 'eloquent',

        'model' => App\Models\User::class,
        'table' => 'users',

    ],

try it out... Sorry if i am wrong..

nr: #3 dodano: 2017-01-03 11:01

But, when i register, it goes to users table. Why and how to set it to the correct table?

Assuming you are using the auto generated auth controllers, views and routes. Navigate to app/Http/Controllers/Auth/RegisterController.php add using YourModelName to the top and change the following function to your own Model.

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

If we use the default laravel authentication , we are not able to set the route?

If u mean redirection after registration you can also set that in the RegisterController

 /**
 * Where to redirect users after registration.
 *
 * @var string
 */
protected $redirectTo = '/home';

About the Auth routes. They are automatically set by the following line in your /routes/web.php

Auth::routes(); 

You can change this by deleting this and replacing it with your desired Auth routes

Source Show
◀ Wstecz