3
votes

In Zizaco/entrust's Laravel Entrust, how do you setup the Entrust class relationships with Eloquent so you can get the list of permissions a role has like so:

// get admin role    
$adminRole = Role::find(1);   

// get the permission of the admin role
$adminRolePermissions = $adminRole->permissions();

I've tried adding the following Eloquent relationships to the Role class:

class Role class {
   public function permissions()
   {
      return $this->hasManyThrough('App\Permission', 'App\PermissionRole', 'role_id', 'permission_id');
   }
}

With this, when I get an instance of a role and try to get its permissions like so:

$adminRolePermissions = $adminRole->permission();

this error occurs:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'permission_role.id' in 'on clause' (SQL: select permissions.*, permission_role.role_id from permissions inner join permission_roles on permission_role.id = permissions.permission_id where permission_role.role_id = 1)

Of course, I have already created the following models: PermissionRole for the permission_role table.

1
Do you actually have a role_permission table ? Consider Eloquent is looking dor permission_role, the convention is alphabetical to name pivot tables pairs.alariva
Sorry about that. I've edited the last portion. I have permission_role table and PermissionRole model.doncadavona
Would you as well add the tables creation migration files? Have you done any change on them? What is happening is that the coulmn permission_role_id is not found in this relationship, and we have to check now if it should exist or the query is incorrect. It is probably related with the hasManyThrough() method usage.alariva
I'm thingking the hasManyHasThrough() is the problem too. I'm thingking it requires a Primary Key on the permission_role table because it doesn't have it.doncadavona

1 Answers

0
votes

permission_role.id is invalid. Your table permission_role should have columns, named "permission_id" and "role_id" by default because of "Eloquent ORM Relationships". May be you have override your Model's Primary Key name. Please check that Config.entrust file has the same attributes as your Models have.