BelongsToThrough

Inverse HasManyThrough relationships for Laravel Eloquent with unlimited intermediate models

Library
Composer
vv2.18
1,269 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
45 /100 Fair
Development Activity 4
Maintenance 32
Community 56
Maturity 60
Momentum 28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
83 /100 Excellent
Architecture 80
Code Quality 88
Innovation 82
Learning Curve 78

BelongsToThrough is a Laravel Eloquent extension that adds the inverse of the built-in HasManyThrough relationship. Where HasManyThrough lets a Country reach its Posts through Users, BelongsToThrough lets a Post reach its Country through Users, chaining through any number of intermediate models.

The package is a single, focused trait you add to your models. It supports unlimited intermediate models, custom foreign and local keys, table aliases for self-referencing chains, and soft-deleting, staying faithful to Eloquent’s relationship conventions.

What You Get

  • A BelongsToThrough trait that defines inverse through-relationships on any Eloquent model
  • Support for unlimited intermediate models in a single relationship chain
  • Custom foreign key and local key configuration per relationship segment
  • Table aliasing so the same table can appear multiple times in a chain
  • Soft-delete awareness consistent with Eloquent’s built-in relationships

Common Use Cases

  • Reaching a Post’s Country through its User without manual joins
  • Traversing deep ownership chains like Comment to Country through Post and User
  • Loading a distant parent relationship with eager loading and constraints
  • Modeling self-referential hierarchies that require table aliases

Under The Hood

Architecture — The package centers on a single Relations/BelongsToThrough.php class extending Eloquent’s Relation, plus a BelongsToThrough trait (src/Traits/) that adds the belongsToThrough() factory method to models. A HasTableAlias trait enables aliasing tables that recur in a chain, and an optional IdeHelperServiceProvider with a relations hook feeds Laravel IDE Helper so the custom relation is autocompleted. The relation builds joins across each intermediate segment and applies keys, aliases, and soft-delete constraints as the chain is assembled.

Tech Stack — Pure PHP with no runtime dependencies beyond illuminate/database, targeting Laravel Eloquent (the README documents support back to Laravel 5.0 across tagged versions). The 100% PHP codebase is validated with PHPUnit, Codecov coverage, and PHPStan at level 10 in CI.

Code Quality — For its small size the project is rigorously maintained: PHPStan level 10 static analysis, a dedicated test suite (tests/BelongsToThroughTest.php with model fixtures for countries, users, posts, comments, and aliased tables), and code coverage reporting. The single-responsibility structure keeps the relation logic isolated and readable.

API Design — The public API mirrors Laravel’s own relationship methods, so belongsToThrough(Country::class, User::class) reads exactly like hasManyThrough. This convention-first design means existing Eloquent developers need almost no new concepts, and the README’s key-customization and alias examples cover the non-trivial cases concisely.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers