Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to determine the type of currently logged-in user? #122

Open
humbertowoody opened this issue Feb 13, 2018 · 3 comments
Open

How to determine the type of currently logged-in user? #122

humbertowoody opened this issue Feb 13, 2018 · 3 comments

Comments

@humbertowoody
Copy link

I'm trying to do something like:

if( Auth::user()->isAdmin ) {
    // some admin stuff...
} else if (Auth::user()->isTeacher ) {
    // some teacher stuff
}

and so on.
Is there a way we can accomplish this?

@toksdotdev
Copy link

toksdotdev commented Feb 17, 2018

Since Auth::user() returns back a user model object, it will be nice if you create probably a function in your User Model class (or as it is named). See a sample.

In your case, it might not be User Model, depending on what you named your provider/ multi-auth.

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class User extends Model 
{
    ... // other model fields and method here

    public function isTeacher() {
        $teacher = App\Teacher::where('email', $this->email)->first();

        return is_null($teacher);
    }
}

@humbertowoody
Copy link
Author

This is definitely a very good answer! And totally works. It is a very nice way of simplifying the stuff on the controllers' code. Maybe it will be worth adding this method when generating model on php artisan multi-auth:install ?
Anyway, thank you very much :)

@toksdotdev
Copy link

You're welcome. I'll do a PR for that feature and hope it gets accepted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants