0

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.

schutte
  • 1,949
  • 7
  • 25
  • 45

2 Answers2

2

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.

Shizzen83
  • 3,325
  • 3
  • 12
  • 32
  • thanks for the idea Sir, now I will not make any modification, I will stick to user(). thanks for helpful suggestion. – schutte Jul 03 '20 at 09:50
-2

Well, never done this before, but you can go to vendor\laravel\framework\src\Illuminate\Auth\SessionGuard.php and change user() to the desired name. Personally, for tests, I would copy the whole function, change to account() but keep users() in case something goes wrong.

hhelderneves
  • 925
  • 8
  • 24
  • 2
    `vendor` directory must not be changed like that because it is gitignored and any update will erase the change. – Shizzen83 Jul 03 '20 at 09:25
  • It's true. But your question points to ```Auth()```. Use the ```app/User``` instead. – hhelderneves Jul 03 '20 at 09:33
  • @hmrneves already tried that, another error returned **Class Illuminate\Auth\SessionGuard contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Illuminate\Contracts\Auth\Guard::user)** – schutte Jul 03 '20 at 09:51
  • It's normal, you are trying to mess with the framework. Please read all the answers and comments. – hhelderneves Jul 03 '20 at 10:04