Search code examples
phplaravellaravel-6laravel-authentication

How to completely change the auth table in laravel?


I have accounts table in which I want to be Authenticatable and I done to this, already did the answer of @PintuKumar in this stackoverflow-link and it's working fine.

My issue here, the line below is working

auth()->user()->email

But what I wanted is the line below, when I try it, it doesn't work

auth()->account()->email

It returns an error below:

Method Illuminate\Auth\SessionGuard::account does not exist. (View: D:\workspace\laravel\blog\resources\views\home.blade.php)

Anyone, does know how to change function name from user to custom? I'm really stuck with this.


Solution

  • The method name does not matter, user method does not mean your authentication guards fetches a App\User, it fetches the entity authenticated by this guard, whatever it is.

    If you really want to do this (notice that it would be nothing more than an alias), you have to inherit your session guard and add this method

    public function account()
    {
        return $this->user();
    }
    

    But once again it is a bad idea, particularly because you ignore a method which is implemented by every guard. If one day you decide to change the used guard, your code will break for the same reason.