Laravel身份验证-不同表中的用户名和密码 [英] Laravel Authentication - Username and Password in different tables

查看:396
本文介绍了Laravel身份验证-不同表中的用户名和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我在不同的表中有用户名和密码,所以在laravel中出现身份验证问题.由于Auth使用相同的表作为用户名和密码,但是我的数据库已经设置好,其中用户名在表users中,密码在表webpages_membership中,而且我无法更改数据库结构,因为该数据库也被其他移动应用程序和网站使用.因此,如何使用Auth进行登录系统.

getting problem with authentication in laravel as I have username and password in different tables.As Auth uses same table for username and password but my database is already setup where the the username is in table users and the password is in table webpages_membership, and I cant change the database structure because that database is used by other mobile application and website too. So how do I use Auth for login system.

@btl:

我尝试了解决方案,但现在又出现了

I tried the solution but now there is another error of

"Undefined index: password" in vendor\laravel\framework\src\Illuminate\Auth\GenericUser.php

以下是我的用户模型代码.

following is my user model code.

代码:

<?php

    namespace App;

    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use App\WebPages_Membership;

    class User extends Authenticatable
    {
        use Notifiable;

        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */

        protected $table = 'Users';

        protected $dbPrefixOveride = '';

        protected $primaryKey = 'UserId';

        protected $fillable = [
            'Username', 'FirstName', 'LastName','Email','MobileNumber','CreatedBy','CreatedDate','ModifiedBy','ModifiedDate','DistributorId','Telephone','IsAuthorized','AlternateMobileNumber','AlternateEmail','IsDeleted','UnauthorizationRemark'
        ];

        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        /*protected $hidden = [
            'password', 'remember_token',
        ];*/

        public function webpagesMembership() {
            return $this->hasOne(WebPages_Membership::class);
        }

        public function getPasswordAttribute() {
            return $this->webpagesMembership->getAttribute('Password');
        }
    }
?>

推荐答案

我会做类似的事情.假设您的表之间是一对一的关系.

I'd do something like this. Assuming a one-to-one relationship between your tables.

定义用户和WebpagesMembership模型之间的关系.您的用户模型将具有以下内容:

Define a relationship between User and WebpagesMembership models. Your User model would have the following:

public function webpagesMembership() {
    return $this->hasOne(WebpagesMembership::class);
}

添加访问器功能

public function getPasswordAttribute() {
    return $this->webpagesMembership->getAttribute('password');
}

以这种方式,当Auth尝试访问您的User模型上的password属性时,它将起作用.

That way Auth will work when it attempts to access the password attribute on your User model.

password添加到用户模型的$appends属性:

Add password to the User model's $appends property:

 protected $appends = [
    'password'
 ];

这就像现在是模型上的属性一样.您遇到的错误是因为在构造函数中设置了GenericUser的属性,而password不存在.然后,它尝试访问以下位置的password:

This will act as if it were an attribute on the model now. The error you encountered was because the GenericUser's attributes were being set in the constructor and password did not exist. It then attempted to access password in:

public function getAuthPassword()
{
    return $this->attributes['password'];
}

因此,未定义的索引.

这篇关于Laravel身份验证-不同表中的用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆