curly-deni/laravel-scopes
Composer 安装命令:
composer require curly-deni/laravel-scopes
包简介
A set of reusable Eloquent global scopes for controlling model visibility based on user ownership and public/private status, designed for seamless integration with Laravel authorization policies.
README 文档
README
Laravel Scopes provides a set of reusable Eloquent global scopes to control model visibility based on user ownership and public/private status.
It is designed for easy integration with Laravel's authorization policies.
Features
- ✅ Simple and lightweight integration
- ✅ Reusable Eloquent scopes for ownership and visibility
- ✅ Works seamlessly with Laravel Policies
- ✅ No manual query building required
- ✅ Clean separation of concerns between data and authorization
Installation
Install the package via Composer:
composer require curly-deni/laravel-scopes
Usage
All traits are located under the Aesis\Scopes\Traits namespace.
Available traits
HasOwnershipScope— restricts access to models owned by the current user (user_idfield required).HasPublicScope— restricts access to public models (publicfield required).HasPrivateScope— restricts access to private models (privatefield required).HasSelfScope— allows users to see their own models in addition to public/private ones (user_idfield required).
Usage Scenarios
1. Restrict models to the owner only
Use HasOwnershipScope when users should only see their own models.
use Aesis\Scopes\Traits\HasOwnershipScope; class Post extends Model { use HasOwnershipScope; }
2. Restrict models based on public/private status
Use HasPublicScope or HasPrivateScope to filter models by their visibility flag.
Optionally, add HasSelfScope to also allow users to see their own models.
use Aesis\Scopes\Traits\HasPublicScope; use Aesis\Scopes\Traits\HasSelfScope; class Post extends Model { use HasPublicScope, HasSelfScope; }
Trait Selection Guide
| Use Case | Required Traits |
|---|---|
| Users should see only their own models | HasOwnershipScope |
| Users should see only public models | HasPublicScope |
| Users should see only private models | HasPrivateScope |
| Users should see public models and their own private models | HasPublicScope + HasSelfScope |
| Users should see private models and their own private models | HasPrivateScope + HasSelfScope |
Database Requirements
| Trait | Required Field |
|---|---|
HasPublicScope |
public |
HasPrivateScope |
private |
HasOwnershipScope |
user_id |
HasSelfScope |
user_id |
Policy Requirements
When using scopes, you should define the following permissions in your model policies:
| Method | Purpose |
|---|---|
viewPrivate(User $user) |
Allows a user to view private models (e.g., admins or users with special roles). |
viewForeign(User $user) |
Allows a user to view other users' models (in addition to their own, if HasSelfScope is used). |
Example policy:
class PostPolicy { public function viewPrivate(User $user): bool { // Allow access to private models for admins return $user->is_admin; } public function viewForeign(User $user): bool { // Allow users to view models owned by others return $user->can('view-others-posts'); } }
Restrictions
HasOwnershipScopecannot be combined with any other traits.HasPublicScopeandHasPrivateScopecannot be used together.HasSelfScoperequires eitherHasPublicScopeorHasPrivateScope.
Credits
License
The MIT License (MIT).
Please see the LICENSE.md file for more information.
Summary
Laravel Scopes help you automatically filter models by ownership or visibility status without writing repetitive query logic.
Combined with authorization policies, it provides flexible and secure access control at the Eloquent level.
curly-deni/laravel-scopes 适用场景与选型建议
curly-deni/laravel-scopes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「visibility」 「laravel」 「eloquent」 「laravel package」 「scopes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 curly-deni/laravel-scopes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 curly-deni/laravel-scopes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 curly-deni/laravel-scopes 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Private and protected classes for PHP.
Laravel JWT auth service package
Propel behavior that helps you to set a visibility per fields
Toggle password visibility.
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-26