PHPackages                             steven-fox/eloquaint - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. steven-fox/eloquaint

ActiveLibrary[Database &amp; ORM](/categories/database)

steven-fox/eloquaint
====================

Reduce the boilerplate in your Eloquent classes.

v0.0.1(9mo ago)10[4 PRs](https://github.com/steven-fox/eloquaint/pulls)MITPHPPHP ^8.3CI passing

Since Aug 7Pushed 2mo agoCompare

[ Source](https://github.com/steven-fox/eloquaint)[ Packagist](https://packagist.org/packages/steven-fox/eloquaint)[ Docs](https://github.com/steven-fox/eloquaint)[ GitHub Sponsors]()[ RSS](/packages/steven-fox-eloquaint/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (12)Versions (6)Used By (0)

Experimental!
=============

[](#experimental)

This project is a work in progress. Expect breaking changes.

Eloquaint - Reduce the boilerplate in your Eloquent classes
===========================================================

[](#eloquaint---reduce-the-boilerplate-in-your-eloquent-classes)

[![Latest Version on Packagist](https://camo.githubusercontent.com/2e907d87499183308302a74206b58a2a0f9b6286066b4ab7c8dc5142b0245978/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73746576656e2d666f782f656c6f717561696e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/steven-fox/eloquaint)[![GitHub Tests Action Status](https://camo.githubusercontent.com/185861fabd88329bd291e78ef8a40a0930b2830426ea4561e9bf3e439d5d577a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73746576656e2d666f782f656c6f717561696e742f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/steven-fox/eloquaint/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/5fe01766d9fd45703cfc9c2e30c81cfff3ee1166c3146d926839c570af8e0e7f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73746576656e2d666f782f656c6f717561696e742f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/steven-fox/eloquaint/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/80d85c661e9d928bb9b8bd6a0f2e385eaabdfb618857baf971f94de15fde7a9b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73746576656e2d666f782f656c6f717561696e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/steven-fox/eloquaint)

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
-----------------------------------

[](#php-85-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', ' 'published'])]
class Author extends Model
{
    use HasEloquaintFeatures;
}

// Usage:
$author->articles; // All posts
$author->publishedArticles; // Only published posts
```

### Query Constraints

[](#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

[](#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

[](#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
----------------

[](#supported-scopes)

Eloquaint also supports defining local scopes using attributes:

### Simple Scopes

[](#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

[](#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

[](#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
------------

[](#how-it-works)

1. **Add the trait**: Include `HasEloquaintFeatures` in your model (or `HasAttributeRelations` for relationships only)
2. **Define relationships and scopes**: Use PHP attributes on your class
3. **Use normally**: Access relationships and scopes exactly like traditional Eloquent

The package automatically:

- Resolves relationship names (e.g., `Post::class` becomes `posts`)
- Handles foreign key conventions
- Applies query constraints and scope logic
- Caches definitions for performance
- Supports both static and instance method calls for scopes

Performance
-----------

[](#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
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Steven Fox](https://github.com/steven-fox)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance74

Regular maintenance activity

Popularity2

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 75% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

284d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c3c9b257c10b41dc9dcad9d29657e430ea0b902c91a0e9392fa9f57bf48bd349?d=identicon)[stevenfox](/maintainers/stevenfox)

---

Top Contributors

[![steven-fox](https://avatars.githubusercontent.com/u/62109327?v=4)](https://github.com/steven-fox "steven-fox (9 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

laravelsteven-foxeloquaint

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/steven-fox-eloquaint/health.svg)

```
[![Health](https://phpackages.com/badges/steven-fox-eloquaint/health.svg)](https://phpackages.com/packages/steven-fox-eloquaint)
```

###  Alternatives

[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spatie/laravel-model-flags

Add flags to Eloquent models

4301.1M1](/packages/spatie-laravel-model-flags)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[spatie/laravel-sql-commenter

Add comments to SQL queries made by Laravel

1931.4M1](/packages/spatie-laravel-sql-commenter)[spatie/laravel-deleted-models

Automatically copy deleted records to a separate table

409109.8k4](/packages/spatie-laravel-deleted-models)[wnx/laravel-backup-restore

A package to restore database backups made with spatie/laravel-backup.

203330.1k2](/packages/wnx-laravel-backup-restore)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
