staudenmeir/belongs-to-through
Larablocks Analysis
Compatibility
Trusted maintainerstaudenmeir/belongs-to-through works with Laravel 13 (latest release v2.18, Feb 28, 2026).
Laravel 4 to 12 in earlier releases.
- Trusted maintainer: staudenmeir publishes 15 packages on Larablocks, with 5,145,651 downloads a month between them.
Recommendation checks
RecommendedRecommended by Larablocks: meets all 4 checks.
- Works with Laravel 13, the current version
- Released in the last 12 months (Feb 28, 2026)
- 618,425 downloads a month (at least 10,000)
- 1,267 GitHub stars (at least 100)
State
Maintained- Last stable release: Feb 28, 2026.
- 1 release in the last 12 months.
- First released: Jun 6, 2015.
- Monthly downloads: 618,425, up 70% from six months ago.
- 0 open issues on GitHub.
Data from Packagist and GitHub, checked by Larablocks on Nov 25, 2026.
| Install | |
|---|---|
composer require staudenmeir/belongs-to-through |
|
| Latest Version: | v2.18 |
| PHP: | ^8.3 |
| Laravel: | 13 |
| Older releases: | 4 – 12 |
| License: | MIT |
| Last Updated: | Feb 28, 2026 |
| Links: | GitHub · Packagist |
BelongsToThrough
This inverse version of HasManyThrough allows BelongsToThrough relationships with unlimited intermediate models.
Supports Laravel 5.0+.
Installation
composer require staudenmeir/belongs-to-through:"^2.5"
Use this command if you are in PowerShell on Windows (e.g. in VS Code):
composer require staudenmeir/belongs-to-through:"^^^^2.5"
Versions
| Laravel | Package |
|---|---|
| 13.x | 2.18 |
| 12.x | 2.17 |
| 11.x | 2.16 |
| 10.x | 2.13 |
| 9.x | 2.12 |
| 8.x | 2.11 |
| 7.x | 2.10 |
| 6.x | 2.6 |
| 5.x | 2.5 |
Usage
Consider this HasManyThrough relationship:
Country → has many → User → has many → Post
class Country extends Model
{
public function posts()
{
return $this->hasManyThrough(Post::class, User::class);
}
}
Use the BelongsToThrough trait in your model to define the inverse relationship:
Post → belongs to → User → belongs to → Country
class Post extends Model
{
use \Znck\Eloquent\Traits\BelongsToThrough;
public function country(): \Znck\Eloquent\Relations\BelongsToThrough
{
return $this->belongsToThrough(Country::class, User::class);
}
}
You can also define deeper relationships:
Comment → belongs to → Post → belongs to → User → belongs to → Country
Supply an array of intermediate models as the second argument, from the related (Country) to the parent model (Comment):
class Comment extends Model
{
use \Znck\Eloquent\Traits\BelongsToThrough;
public function country(): \Znck\Eloquent\Relations\BelongsToThrough
{
return $this->belongsToThrough(Country::class, [User::class, Post::class]);
}
}
Custom Foreign Keys
You can specify custom foreign keys as the fifth argument:
class Comment extends Model
{
use \Znck\Eloquent\Traits\BelongsToThrough;
public function country(): \Znck\Eloquent\Relations\BelongsToThrough
{
return $this->belongsToThrough(
Country::class,
[User::class, Post::class],
foreignKeyLookup: [User::class => 'custom_user_id']
);
}
}
Custom Local Keys
You can specify custom local keys for the relations:
VendorCustomerAddress → belongs to → VendorCustomer in VendorCustomerAddress.vendor_customer_id
VendorCustomerAddress → belongs to → CustomerAddress in VendorCustomerAddress.address_id
You can access VendorCustomer from CustomerAddress by the following
class CustomerAddress extends Model
{
use \Znck\Eloquent\Traits\BelongsToThrough;
public function vendorCustomer(): \Znck\Eloquent\Relations\BelongsToThrough
{
return $this->belongsToThrough(
VendorCustomer::class,
VendorCustomerAddress::class,
foreignKeyLookup: [VendorCustomerAddress::class => 'id'],
localKeyLookup: [VendorCustomerAddress::class => 'address_id'],
);
}
}
Table Aliases
If your relationship path contains the same model multiple times, you can specify a table alias (Laravel 6+):
class Comment extends Model
{
use \Znck\Eloquent\Traits\BelongsToThrough;
public function grandparent(): \Znck\Eloquent\Relations\BelongsToThrough
{
return $this->belongsToThrough(
Comment::class,
Comment::class . ' as alias',
foreignKeyLookup: [Comment::class => 'parent_id']
);
}
}
Use the HasTableAlias trait in the models you are aliasing:
class Comment extends Model
{
use \Znck\Eloquent\Traits\HasTableAlias;
}
Soft Deleting
By default, soft-deleted intermediate models will be excluded from the result. Use withTrashed() to include them:
class Comment extends Model
{
use \Znck\Eloquent\Traits\BelongsToThrough;
public function country(): \Znck\Eloquent\Relations\BelongsToThrough
{
return $this->belongsToThrough(Country::class, [User::class, Post::class])
->withTrashed('users.deleted_at');
}
}
class User extends Model
{
use SoftDeletes;
}
Contributing
Please see CONTRIBUTING and CODE OF CONDUCT for details.
Credits
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v2.18 | ^8.3 | ^13.0 | MIT | |
| v2.17 | ^8.2 | ^12.0 | MIT | |
| v2.16.4 | ^8.2 | ^11.0 | MIT | |
| v2.16.3 | ^8.2 | ^11.0 | MIT | |
| v2.16.2 | ^8.2 | ^11.0 | MIT | |
| v2.16.1 | ^8.2 | ^11.0 | MIT | |
| v2.16 | ^8.2 | ^11.0 | MIT | |
| v2.15.1 | ^8.1 | ^10.0 | MIT | |
| v2.15 | ^8.1 | ^10.0 | MIT | |
| v2.14 | ^8.1 | ^10.0 | MIT | |
| v2.13 | ^8.1 | ^10.0 | MIT | |
| v2.12.1 | ^8.0.2 | ^9.0 | MIT | |
| v2.12 | ^8.0.2 | ^9.0 | MIT | |
| v2.11.2 | ^7.3| | ^8.0 | MIT | |
| v2.11.1 | ^7.3| | ^8.0 | MIT | |
| v2.11 | ^7.3 | ^8.0 | MIT | |
| v2.10.1 | ^7.2.5| | ^7.0 | MIT | |
| v2.10 | ^7.2.5 | ^7.0 | MIT | |
| v2.9.1 | ^7.2| | ^6.0 | MIT | |
| v2.9 | ^7.2 | ^6.0 | MIT | |
| v2.8 | ^7.2 | ^6.0 | MIT | |
| v2.7 | ^7.2 | ^6.0 | MIT | |
| v2.6 | ^7.2 | ^6.0 | MIT | |
| v2.5.1 | >=5.6 | ~5.0 | MIT | |
| v2.5 | >=5.6 | ~5.0 | MIT | |
| v2.4.2 | >=5.6 | ~5.0 | MIT | |
| v2.4.1 | >=5.6 | ~5.0 | MIT | |
| v2.4 | >=5.6 | ~5.0 | MIT | |
| v2.3.2 | ~5.0 | MIT | ||
| v2.3.1 | ~5.0 | MIT |
Showing the latest 30 of 38. See every release on Packagist
Pre-releases (1)
| Version | Released | Laravel | License |
|---|---|---|---|
| v2.0-alpha | >4.0 | MIT |