JosephSilber / bouncer

Laravel Eloquent roles and abilities.
MIT License
3.43k stars 330 forks source link

How to get owns and check is owned #648

Open bilginnet opened 4 months ago

bilginnet commented 4 months ago

In laravel 11 I am assigning ownership to an organization like below.

$organization = Organization::create(['name' => 'Test Org']); Bouncer::allow($user)->toOwn($organization);

Question 1: How can I see which organizations I have ownership rights after logging in. Ex: $user->owns()

Question 2: How do I check if the user has ownership rights on any organization after login? Ex: $user->isOwnedFor($organization)

EriBloo commented 3 months ago

Since ownership is handled by key on the Organization model, you could just use relation for this. Ownership checks if Organization has user_id (by default) with value of authenticated user. If so, that user owns this model.