Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

safin's avatar
Level 1

How to use more than one table to auth in Laravel 5.6 ?

This is my tables structure i want to make auth in laravel 5.6 :

permission: permission_id , permission

login: login_id , permission_id , Full_name , password

email: email_id , login_id , email

phone: phone_id , login_id , phone_number

users can login with email and password or phone with password each user can have many phone and email and I have 3 type of permission user , admin , manager. how can I do that ?

0 likes
5 replies
Galavant's avatar

I ussualy use the following project for roles Laratrust

However, if you want to check for certain values for people that are logging in, you can do the following:

On your login view, redirect the login button to another route, for example auth/login

after that, make a new route in your web.php Route::post('/auth/login', 'AccountController@authenticate'); (make specific controller for this or an already existing controller)

In that controller you can now do the authentication yourself

use Illuminate\Support\Facades\Auth;

class AccountController extends Controller
{
public function authenticate(Request $request)
    {
        // GET PUT IN INFO
        $email = $request->email;
        $password = $request->password;
        
        // ATTEMPTING THE LOGIN
        if (Auth::attempt(['email' => $email, 'password' => $password, 'role' => 0])){
            // Authentication passed... Role is 0 

            return redirect()->intended('/dashboard');
        }else{
            return Redirect::back()->withError('Your details are incorrect or your role doesn't match the current structures');
        }
    }
}

If you want to connect more tables to your users, you can use relationships for that

Make a new column named emails

Emails

id  - user_id - Email

In the user model you can now put

class User extends Authenticatable
{

    public function emails()
    {
        return $this->hasMany('App\Email','user_id', 'id');
    }
}

Take a look into relationships

relationships

shez1983's avatar

basically what you need to do is:

override the Trait's (AuthenticatesUsers) function ( login ) that your App\Http\Controllers\Auth\LoginController uses..

you can basically copy/paste all of the functionality the only thing you need to override is this bit:

if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }```

where you can add an else if to check your phone table.. or override attemptLogin that does all that for you (ie first check login tbl & then phones..)


safin's avatar
Level 1

this is my function it work if only the phone and email field was in the login table in my case login and phone in another table i have a login table with login_id ,password,permission and email table with email_id ,login_id, email and phone table with phone_id,login_id, phone_number

login_id in both phone and email table is foreign key so each user have many email used for login i must join email table and login table and check email and password how can i do this ??

protected function attemptLogin(Request $request) { return $this->guard()->attempt( $this->credentials($request), $request->filled('remember') ); }

safin's avatar
safin
OP
Best Answer
Level 1

thanks for all for replays i solve it and this is my code what id did is i make login with login_id and password but i get login_id from email or phone table so i get the login_id of email that user entered

Please or to participate in this conversation.