Laravel Authentication - Username and Password in different tables

后端 未结 1 1315
南方客
南方客 2021-01-27 06:17

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 s

相关标签:
1条回答
  • 2021-01-27 06:43

    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.

    0 讨论(0)
提交回复
热议问题