Skip to content

[12.x] feat: create UsePolicy attribute #55882

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

Merged
merged 2 commits into from
Jun 4, 2025

Conversation

calebdw
Copy link
Contributor

@calebdw calebdw commented May 28, 2025

Hello!

This was bundled in #55765, but I'm splitting out into its own PR. Renamed from PolicedBy to UsePolicy.

#[UsePolicy(PostPolicy::class)]
class Post extends Model
{
}

Thanks!

@ahinkle
Copy link
Contributor

ahinkle commented May 29, 2025

Hi @calebdw 👋

What’s the use case here? Policy auto-discovery already works quite well, whereas the attributes of adding scopes or observers is pretty clear.

@calebdw
Copy link
Contributor Author

calebdw commented May 29, 2025

@ahinkle,

The use case is to create an explicit connection between the model and policy instead of relying on magic naming conventions and placement (just like with UseFactory instead of just relying on the factory being in the right place).

@taylorotwell taylorotwell merged commit 8da277b into laravel:12.x Jun 4, 2025
59 checks passed
@calebdw calebdw deleted the calebdw/push-xqpqlvmzpqqv branch June 4, 2025 17:39
@peterjthomson
Copy link

Our proposal for this was more complicated so UsePolicy makes perfect sense. We'll re-visit the cross-model policy mapping customisation and/or the closed-by default pattern in light of the new PR. Great to see Policies getting some love! https://github.com/icehouse-ventures/laravel-policy-attributes

AhmedAlaa4611 referenced this pull request in laravel/docs Jun 11, 2025
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

Successfully merging this pull request may close these issues.

4 participants