ollieread / multiauth

Laravel multi auth
441 stars 109 forks source link

Getting a specific field of auth() #51

Closed notflip closed 10 years ago

notflip commented 10 years ago

Hi,

I'm using multiauth to login company's and / or admins. After logging in I would like to retrieve the ID of the company that is logged in..

$id = Auth::company()->get('id');

This is not working, Any idea how to do it? Thanks! great plugin

SOLUTION

$company = Auth::company()->get()->id;

mikkezavala commented 10 years ago

Not quite understand you question but, i assume you have already defined the model properly in the db, so why dont just call the field name like in the native auth lib?

//Native
$id = Auth::user()->id;
$name = Auth::user()->name;
//MultiAuth
$id = Auth::company()->user()->id;
$name = Auth::company()->user()->name;
.....
ollieread commented 10 years ago

There's a lot of confusion regarding this, the actual first part you call is the specified name, so Auth::company() isn't the company model, but is instead the instance of Guard for this specific setting.

As @mikkezavala mentioned above, you can call Auth::company()->user()->id, or alternatively you can call Auth::company()->get()->id, where get is a simple method I added to avoid confusion.