New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

znck/belongs-to-through

Laravel Eloquent BelongsToThrough relationships
230,079 1,268

Larablocks Analysis

znck/belongs-to-through works with Laravel 8 (latest release v2.11.2, Aug 19, 2021), which reached end of life on Jan 24, 2023: no more fixes, even for security.

Laravel 4 to 7 in earlier releases.

Recommendation checks

Replacement recommended

Replacement recommended: use staudenmeir/belongs-to-through instead. Named by its author on Packagist.

  • Marked abandoned on Packagist
  • Supports only Laravel 8, past end of life since Jan 24, 2023
  • No release in the last 12 months (last: Aug 19, 2021)
  • 1,018 downloads a month (needs 10,000)
  • 1,268 GitHub stars (at least 100)

State

Abandoned
  • Packagist marks this package abandoned.
  • Last stable release: Aug 19, 2021.
  • No releases in the last 12 months.
  • First released: Jun 6, 2015.
  • Monthly downloads: 1,018.
  • 0 open issues on GitHub.

Data from Packagist and GitHub, checked by Larablocks on Dec 3, 2026.

Install
composer require znck/belongs-to-through
Latest Version:v2.11.2
PHP:^7.3|^8.0
Laravel:8
Older releases:4 – 7
License:MIT
Last Updated:Feb 28, 2026
Links: GitHub  ·  Packagist
Maintainer: znck

BelongsToThrough

CI Code Coverage PHPStan Latest Stable Version Total Downloads License

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

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

670,032,257 9,703
laravel/framework

The Laravel Framework.

640,572,096 35,338
laravel/tinker

Powerful REPL for the Laravel framework.

546,909,908 7,440
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

465,816,364 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

431,187,642 4,660

Version History

Version Released PHP Laravel License
v2.11.2 ^7.3|^8.0 ^8.0 MIT
v2.11.1 ^7.3|^8.0 ^8.0 MIT
v2.11 ^7.3 ^8.0 MIT
v2.10.1 ^7.2.5|^8.0 ^7.0 MIT
v2.10 ^7.2.5 ^7.0 MIT
Pre-releases (1)
Version Released Laravel License
v2.0-alpha >4.0 MIT