Laravel-permission: Unable to retrieve roles of user...

Created on 14 Dec 2017  路  13Comments  路  Source: spatie/laravel-permission

In my Controller:

$whichuser = \App\User::find($theuserID);
$roles = $whichuser->getRoleNames();

In My User Model:

use Spatie\Permission\Traits\HasRoles;
and 
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use HasRoles;

And when I run the controller I get

Call to undefined method Illuminate\Database\Query\Builder::getRoleNames()...I need to check the role on the user:

when I echo out the user from the which user call I get:

{"id":8,"name":"Test User1","email":"[email protected]","created_at":"2017-11-28 21:33:59","updated_at":"2017-11-28 21:33:59"}

What is interesting is that:
$doeshave = $whichuser->hasRole('admin');
does return 1 or null depending on the role I put in there..so that appears to be working.

Most helpful comment

Add this to your User Model
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\AuthUser as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
use Notifiable, HasRoles;

All 13 comments

Call to undefined method Illuminate\Database\Query\Builder::getRoleNames()

That's telling you you've got an instance of the Query\Builder, not a User.

You need a User in order to run getRoleNames()

OK thanks.
So I have the User model but I also have a UserSetting model which contains extra information about the user.
Could you enlighten me as how to get back the combined data from both models and then look up what role the User has. I'm just not making the connection between putting the three of these together.
I thought ::find would return a model and not a query builder instance.

Yes, ::find() is supposed to return the model object if successful, else null.
Not sure why you're getting a querybuilder instance, unless you've created a find() method in your User class.

$user = \App\User::with('roles')->find($id); will normally fetch the user by $id along with all their roles. Kinda like the example in #589

Ok so no way have I been able to return the model object....

$user = \AppUser::where('id','=',\Auth::id());
or $user = \Auth::user();
or anything else I use doesn't return a $user which I can use

\Log::info('Userinfo:'.$user->getRoleNames());

to get the role of the user.

nope...

Call to undefined method Illuminate\DatabaseQuery\Builder::getRoleNames()

Can anyone enlighten me on how to do this... very frustrating.

Something's wrong with your User model ... which is causing it to return a query builder object instead of a model instance.

Here's an example of it working in a simple Laravel app: https://github.com/drbyte/spatie-permissions-demo/commit/a722ad2820c1c667fbe5b5c2fd6db148cde499ca

Did this method not exist in the 1 branch? only in the 2?

No, it was never backported to the v1 branch.

It's easily done in your own code though:

$user = \App\User::find(1);
$roles = $user->roles->pluck('name');

note: $user must still be an instance of a User model, not of Query Builder

@drbyte I'm using the api guard as my default. When I created a role, create a permission and assign the permission to the role like so,

        $role = Spatie\Permission\Models\Role::create(['name' => 'writer']);
        $permission = Spatie\Permission\Models\Permission::create(['name' => 'edit articles']);

when I tried

$user = auth()->guard()->user();
 $user->assignRole('writer');

I get No role named writer. But I can see the role in the roles table with guard name as api.

@ultrasamad similar to my reply to your post in #383, it would be helpful if these code examples (and actually even more, such as your code to configure your guard, your User model, etc) were part of a new issue for your Lumen-specific issues. A link to a github repo where we can look at your code would be easiest to support you with.

Add this to your User Model
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\AuthUser as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
use Notifiable, HasRoles;

Just for future reference, I was having issues with querying user roles, and the problem was I had 2 different User Models, my \App\Admin\User Model extended the base \App\User Model, the model_type stored in the user_has_roles table was the child, and I was trying to access it via the parent Model.

Long story short, make sure you're using the right model_type 馃憤

Base table or view not found: 1146 Table 'lynminsuper.permissions' doesn't exist. please I need your help. lynminsuper is my Database

Table 'permissions' doesn't exist

Usually "table doesn't exist" means you haven't published and run the migrations.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ionesculiviucristian picture ionesculiviucristian  路  4Comments

holymp2006 picture holymp2006  路  4Comments

younus93 picture younus93  路  4Comments

ghost picture ghost  路  3Comments

dylangeorgeharbour picture dylangeorgeharbour  路  3Comments