0

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

enter image description here

1 Answer 1

2

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.

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

9 Comments

Thank you for quick reply. Can you help me with where do i write public function getPasswordAttribute()?
Add that to the User model also.
Can you edit your question with the user model formatted? Much easier to read.
I just noticed you have password in the $hidden property array. Can you try removing it from there?
but it is commented code, same error "undefined index:password"
|

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.