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

Add method in Role model #13

Closed
swimlappy opened this issue Dec 1, 2015 · 3 comments
Closed

Add method in Role model #13

swimlappy opened this issue Dec 1, 2015 · 3 comments

Comments

@swimlappy
Copy link

Hello,

Would it be possible to have a scope method added to the Role model to return a list of available roles? Something like this for example

    public function scopeOrderedList($query)
    {
        return $query->orderBy('name')->lists('name', 'id');
    }

For example, this would be useful when building select boxes for someone to choose a role from.

$roles = Role::orderedList()->toArray();

Then pass that into a view.

Thanks

@JosephSilber
Copy link
Owner

You can add your own methods to the role model by extending it:

namespace App;

use Silber\Bouncer\Database\Role as BaseRole;

class Role extends BaseRole
{
    public function scopeOrderedList($query)
    {
        return $query->orderBy('name')->lists('name', 'id');
    }
}

Once you've created your own extended model, simply tell Bouncer to use it:

Bouncer::useRoleModel(\App\Role::class);

Stick that into your AppServiceProvider's boot method, and you're good to go!

@swimlappy
Copy link
Author

Excellent! That keeps things clean. Thanks! And I really like Bouncer, perfect for an app I'm working on!

@dhcmega
Copy link

dhcmega commented Oct 16, 2017

Hi, is this still the way to go? Because with last versions (Laravel 5.5.14, Bouncer v1.0.0-beta.4) if I change the RoleModel with Bouncer::useRoleModel(\App\Role::class), I get "This action is unauthorized".

Thanks!
dhcmega

Edit: The problem was that the permission table had "roles" for entity_type, and now it should be "App\Modes\Role", which is my namespace. Odd that it wasn't originally "Silber\Bouncer\Database\Role"

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

3 participants