Laravel Authentication - Username and Password in different tables

China☆狼群 提交于 2019-12-31 05:29:28

问题


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.

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');
        }
    }
?>


回答1:


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

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

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

Add an accessor function

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

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

Edit:

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

 protected $appends = [
    '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'];
}

Hence, the undefined index.



来源:https://stackoverflow.com/questions/48339615/laravel-authentication-username-and-password-in-different-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!