1

Laravel supported driver "database" or "eloquent" to authorize website . In default config/auth.php we can see it alway declare drive is eloquent.

    ``` 
       /*
        |--------------------------------------------------------------------------
        | User Providers
        |--------------------------------------------------------------------------
        |
        | All authentication drivers have a user provider. This defines how the
        | users are actually retrieved out of your database or other storage
        | mechanisms used by this application to persist your user's data.
        |
        | If you have multiple user tables or models you may configure multiple
        | sources which represent each model / table. These sources may then
        | be assigned to any extra authentication guards you have defined.
        |
        | Supported: "database", "eloquent"
        |
        */
    
        'providers' => [
            'users' => [
                'driver' => 'eloquent',
                'model' => App\Models\Users::class,
            ],
    
            // 'users' => [
            //     'driver' => 'database',
            //     'table' => 'users',
            // ],
        ],
    ```

Then we have a mode User refer with table User to check authentication. So we can using some method of auth : auth::check(),auth::atemp(),auth:login(),... If I dont using Model App\Models\Users::class,. I using 'driver' => 'database'. How i can auhorize by using some function of auth ,

2
  • 1
    then you would have to use database driver and declare the table in the configuration Commented Sep 12, 2022 at 16:48
  • i want to use dirver database because i have multi DB . I think it difficult to auth that understand table which declared Belong DB A or DB B Commented Sep 12, 2022 at 17:25

1 Answer 1

1

Just change the drive to database, you can easily comment out the eloquent part and uncomment the driver database section, you can use auth() normally as you were using before. Laravel's auth works out of the box.

'users' => [
    'driver' => 'database',
    'table' => 'users', //Or whatever table you are using for users.
]

You can design your signIn method in your AuthController, like bellow:

public function signIn(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            'password' => 'required'
        ]);

        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials)) {
            return redirect('/');
        }

        return redirect('login')->withErrors('Login details are invalid');
    }

It will work in both eloquent and database drivers.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks you. but if I use many databases, how can auth understand the table that I declare to use for which database?. Example : In .Env I have connect to DB A and when login i will conect to DB A to get infor DB B, then I connect to DB B have table user . How auth can't understand that table user locate in DB B.
Can I see your .env and config/database.php?
in ENV DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=DB_A DB_USERNAME=root DB_PASSWORD=123 in file config/database.php: 'default' => env('DB_CONNECTION', 'mysql'), 'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''),
when login . i will get infor DB_B (host,database,URL, username , pasword,...) from table DBtaget from databse DB_A . so i conect to DB_B by function mysqli_connect from PHP
Well, while working with multiple databases, there are many factors to be considered and it is hard to explain them in the comment box. In you .env I don't even see a second database connection? Please visit this link that is a detailed explaination of how to work with multiple databases in laravel: codelapan.com/post/…
|

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.