JosephSilber/bouncer

Problem with custom ability model

robertoperez-digital opened this issue · 2 comments

I made a custom ability model following the docs, It extends from Silber\Bouncer\Database\Ability;
when I try to give user a permission like this

Bouncer::allow($user)->to('permission', $entity)

I got
Column not found: 1054 Unknown column 'permissions.custom_ability_id'

In my provider I set table name 'abilities' => 'custom_abilities', I run my migration and again I get
Column not found: 1054 Unknown column 'permissions.custom_ability_id'
When I try to give a permission

I changed migration file to this
Schema::create(Models::table('permissions'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('custom_ability_id')->unsigned()->index();

And I can give the user the permission but when I call
$user->can('permiso', $entity)

I got
Column not found: 1054 Unknown column 'permissions.ability_id'

Am I missing something?

Laravel guesses the foreign IDs from your model's class name.

I just updated the readme with this note:

Eloquent determines the foreign key of relationships based on the parent model name (see the Eloquent docs). To keep things simple, name your custom classes the same as Bouncer's: Ability and Role, respectively.

If you need to use different names, be sure to either update your migration file or override the relationship methods to explicitly set their foreign keys.

OK, so I made this

use Silber\Bouncer\Database\Ability as BAbility; //to avoid errors when extending from Ability

class Ability extends BAbility
{
...
This way my custom ability class can have the same name as the bouncer ability class with no problems

Everything works as expected, thank you!