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

Checking if user owns a model via keys of a pivot table #603

Open
ches-001 opened this issue Aug 13, 2022 · 1 comment
Open

Checking if user owns a model via keys of a pivot table #603

ches-001 opened this issue Aug 13, 2022 · 1 comment

Comments

@ches-001
Copy link

Good day @JosephSilber, I got started with bouncer fairly recently and its a really great tool. It has really been helpful.

So lately I've been trying to implement something, I've got this user table and a business table, a single owner can own one or multiple businesses and a single business can be owned by one or multiple owners, so basically the relationship between these two tables is "many to many relationship". So I've got this pivot table that maps businesses to users and in my Business.php and User.php Models I've set up the relationships with the BelongsToMany() method.

Now, my question is, is their anyway for Bouncer to check if a Business model is owned by a given User, given that their is no user_id column on the business table, this would be impossible with something like Bouncer::can($ability, $business), so Is their a way I could do this, by let's say passing the pivot table and the corresponding keys to a method?

I've tried using the Bouncer::ownedVia() method and passed in a callback function that checks if a user owns a business and so on, but I honestly want to know if their is a better way for this.

@ches-001
Copy link
Author

So I created a policy class called BusinessPolicy and in it I wrote a method called manage which is the name of the ability pertaining to the business model, now surprisingly this ran fine and I didn't even have to register the policy any where, I read the documentation and I saw nothing about this behavior so I would like to know how this works @JosephSilber
Thank you

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

1 participant