How to create login with email from related table on Laravel?

99 views Asked by At

I have 2 tables with the names of users and admins.

users: id, username, password, admin_id

admins: id, email

I use login with laravel UI with default username from the users table. I want to do the login auth with username and email. The username is stored in the users table and the email is in the admins table

public function username()
{
    $login = request()->login;
    $field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
    if ($field == 'username') {
        User::where('username', $login)->first();
        request()->merge([$field => $login]);
        
        return $field;
    } else {
        User::whereHas('admin', function ($q) use ($login) {
            $q->where('email', $login);
        })->first();
        request()->merge([$field => $login]);
        
        return $field;
    }
}
0

There are 0 answers