P粉3511384622023-09-04 12:39:52
You can get the Authenticatable model from the default guard by calling auth()->user()
.
Let’s take a look at the default config/auth.php
<?php return [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', 'hash' => false, ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ] ] ];
With this default Laravel configuration, you get:
web
web
The guard provider (providing Authenticatable
) is users
users
The provider provides App\Models\User::classAuthenticatable
ContractThen, by calling auth()->user()
- you will get an instance of App\Models\User::class or null
You can add anything to the User model (e.g. full_name
) and retrieve it as auth()->user()->full_name
Accessors - using it you can simply add computed properties:
class User extends Authenticatable { public function getFullNameAttribute() { return "{$this->first_name} {$this->last_name}"; } }