Dan.
Dan.

Reputation: 717

Auth - get model

After using the Illuminate\Auth\Authenticatable trait on a model, I can now do Auth::id() in places in my app (when the current auth-ed thing is that particular model).

Is there a way to get the class / type of the auth-ed model?

Perhaps something like Auth::model() which might return the model's class name (such as App\Models\User or App\Models\MyCustomAuthCapabaleModel)?

Upvotes: 0

Views: 711

Answers (3)

Justice Ekemezie
Justice Ekemezie

Reputation: 21

You can get the class or type of the currently authenticated model by accessing the Auth::user() method and using PHP's get_class() function.

use Illuminate\Support\Facades\Auth;

$class = get_class(Auth::user());

// Example output: "App\Models\User" or "App\Models\MyCustomAuthCapabaleModel"

This way, you ensure you retrieve the correct model class associated with the specific guard you are using.

Upvotes: 0

Techno
Techno

Reputation: 1696

Auth::user(); returns the model of the logged in user.

If you ever wish to change the User model, you can change it in config/auth.php at the key providers.users.model

Upvotes: 1

Delano van londen
Delano van londen

Reputation: 416

Auth::user(); returns all the information about the authenticated user

Upvotes: 0

Related Questions