steven-fox/eloquaint
Composer 安装命令:
composer require steven-fox/eloquaint
包简介
Reduce the boilerplate in your Eloquent classes.
README 文档
README
This project is a work in progress. Expect breaking changes.
Eloquaint - Reduce the boilerplate in your Eloquent classes
Eloquaint allows you to define Laravel Eloquent model relationships and scopes using PHP attributes instead of traditional methods, reducing boilerplate code.
PHP 8.5 Closures as Constant Values
In PHP 8.5, it will be possible to define a closure as a "constant value". In theory, this will enable syntax like:
#[Scope('published', static function ($query) {$query->whereNotNull('published_at')->where('published_at', '<=', now())})] #[HasMany(Notification::class, 'readNotifications', static function ($query) {$query->whereNotNull('read_at')})]
Thus, we will incorporate these features once PHP 8.5 hits GA and tag a v1.0 release of this package.
Installation
You can install the package via composer:
composer require steven-fox/eloquaint
Basic Usage
Instead of writing traditional relationship and scope methods:
class Author extends Model { public function posts(): HasMany { return $this->hasMany(Post::class); } public function publishedPosts(): HasMany { return $this->hasMany(Post::class)->where('published', true); } public function scopeActive($query) { return $query->where('active', true); } }
You can now use attributes:
use StevenFox\Eloquaint\Attributes\HasMany; use StevenFox\Eloquaint\Attributes\Scope; use StevenFox\Eloquaint\Traits\HasEloquaintFeatures; #[HasMany(Post::class)] #[HasMany(Post::class, name: 'publishedPosts', where: ['published' => true])] #[Scope('active', 'active', true)] class Author extends Model { use HasEloquaintFeatures; // That's it! No boilerplate methods needed. }
Supported Relationships
Eloquaint supports all Laravel relationship types:
One-to-Many Relationships
use StevenFox\Eloquaint\Attributes\HasMany; #[HasMany(Post::class)] #[HasMany(Comment::class)] class Author extends Model { use HasEloquaintFeatures; }
One-to-One Relationships
use StevenFox\Eloquaint\Attributes\HasOne; #[HasOne(Profile::class)] class User extends Model { use HasEloquaintFeatures; }
Inverse Relationships
use StevenFox\Eloquaint\Attributes\BelongsTo; #[BelongsTo(Author::class)] #[BelongsTo(Category::class)] class Post extends Model { use HasEloquaintFeatures; }
Many-to-Many Relationships
use StevenFox\Eloquaint\Attributes\BelongsToMany; #[BelongsToMany(Tag::class)] #[BelongsToMany(Category::class, table: 'post_categories')] class Post extends Model { use HasEloquaintFeatures; }
Advanced Relationships
use StevenFox\Eloquaint\Attributes\HasManyThrough; use StevenFox\Eloquaint\Attributes\MorphMany; #[HasManyThrough(Comment::class, through: Post::class)] #[MorphMany(Image::class, name: 'imageable')] class Author extends Model { use HasEloquaintFeatures; }
Advanced Features
Custom Relationship Names
#[HasMany(Post::class, name: 'articles')] #[HasMany(Post::class, name: 'publishedArticles', where: ['status' => 'published'])] class Author extends Model { use HasEloquaintFeatures; } // Usage: $author->articles; // All posts $author->publishedArticles; // Only published posts
Query Constraints
Add where clauses directly to your relationship definitions:
#[HasMany(Post::class, where: ['published' => true, 'featured' => true])] class Author extends Model { use HasEloquaintFeatures; }
Custom Foreign Keys
#[BelongsTo(User::class, foreignKey: 'user_id', ownerKey: 'id')] #[HasMany(Comment::class, foreignKey: 'post_id', localKey: 'id')] class Post extends Model { use HasEloquaintFeatures; }
Property-Level Attributes
You can also define relationships on properties:
class Author extends Model { use HasEloquaintFeatures; #[HasMany(Post::class)] protected $posts; #[HasMany(Post::class, where: ['published' => true])] protected $publishedPosts; }
Supported Scopes
Eloquaint also supports defining local scopes using attributes:
Simple Scopes
For basic where clauses, you can define scopes directly:
use StevenFox\Eloquaint\Attributes\Scope; #[Scope('published', 'published', true)] // WHERE published = true #[Scope('draft', 'published', false)] // WHERE published = false #[Scope('popular', 'views', '>', 1000)] // WHERE views > 1000 class Post extends Model { use HasEloquaintFeatures; } // Usage $publishedPosts = Post::published()->get(); $popularPosts = Post::popular()->get();
Complex Scopes
For complex logic, use traditional scope methods alongside simple attribute scopes:
#[Scope('published', 'published', true)] #[Scope('popular', 'views', '>', 1000)] class Post extends Model { use HasEloquaintFeatures; // Use traditional scope methods for complex logic public function scopeRecent($query, $days = 7) { return $query->where('created_at', '>=', now()->subDays($days)); } public function scopeTrending($query) { return $query->where('views', '>=', 1000)->where('likes', '>=', 10); } } // Usage $publishedPosts = Post::published()->get(); // Attribute scope $popularPosts = Post::popular()->get(); // Attribute scope $recentPosts = Post::recent(14)->get(); // Traditional scope $trendingPosts = Post::trending()->get(); // Traditional scope
Chaining with Query Methods
Scopes can be chained with regular query methods:
$posts = Post::published() ->where('title', 'like', '%Laravel%') ->with('author') ->orderBy('created_at', 'desc') ->get(); // For multiple scopes, apply them to the base query $recentPublishedPosts = Post::published()->where('created_at', '>=', now()->subDays(7))->get(); $popularPosts = Post::popular()->get();
How It Works
- Add the trait: Include
HasEloquaintFeaturesin your model (orHasAttributeRelationsfor relationships only) - Define relationships and scopes: Use PHP attributes on your class
- Use normally: Access relationships and scopes exactly like traditional Eloquent
The package automatically:
- Resolves relationship names (e.g.,
Post::classbecomesposts) - Handles foreign key conventions
- Applies query constraints and scope logic
- Caches definitions for performance
- Supports both static and instance method calls for scopes
Performance
Eloquaint is designed for performance:
- Relationship and scope definitions are cached after first resolution
- No runtime overhead compared to traditional relationships and scopes
- Lazy loading and eager loading work exactly the same
- All Eloquent relationship and scope features are preserved
- Scopes work with both static and instance calls
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
steven-fox/eloquaint 适用场景与选型建议
steven-fox/eloquaint 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Steven Fox」 「eloquaint」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 steven-fox/eloquaint 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 steven-fox/eloquaint 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 steven-fox/eloquaint 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
UEditor for laravel5. Support i18n. UEditor is a Rich Text Web Editor From Baidu.
Forked from stevenyangecho/laravel-u-editor.Test UEditor for laravel6. Support i18n. UEditor is a Rich Text Web Editor From Baidu.
Alfabank REST API integration
UEditor for laravel5. Support i18n. UEditor is a Rich Text Web Editor From Baidu.
A PHPUnit results printer that works with the TOON format
Salvation for your model validation.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-07