2017 © Pedro Pelรกez
 

library belongs-to-through

Adds belongsToThrough relation to laravel models

image

znck/belongs-to-through

Adds belongsToThrough relation to laravel models

  • Sunday, July 23, 2017
  • by znck
  • Repository
  • 9 Watchers
  • 130 Stars
  • 84,937 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 15 Forks
  • 4 Open issues
  • 11 Versions
  • 18 % Grown

The README.md

BelongsToThrough

CI Code Coverage PHPStan Latest Stable Version Total Downloads License, (*1)

This inverse version of HasManyThrough allows BelongsToThrough relationships with unlimited intermediate models., (*2)

Supports Laravel 5.0+., (*3)

Installation

composer require staudenmeir/belongs-to-through:"^2.5"

Use this command if you are in PowerShell on Windows (e.g. in VS Code):, (*4)

composer require staudenmeir/belongs-to-through:"^^^^2.5"

Versions

Laravel Package
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, (*5)

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, (*6)

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, (*7)

Supply an array of intermediate models as the second argument, from the related (Country) to the parent model (Comment):, (*8)

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:, (*9)

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:, (*10)

VendorCustomerAddress โ†’ belongs to โ†’ VendorCustomer in VendorCustomerAddress.vendor_customer_id VendorCustomerAddress โ†’ belongs to โ†’ CustomerAddress in VendorCustomerAddress.address_id, (*11)

You can access VendorCustomer from CustomerAddress by the following, (*12)

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+):, (*13)

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:, (*14)

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:, (*15)

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., (*16)

Credits

The Versions

23/07 2017

dev-master

9999999-dev https://github.com/znck/belongs-to-through

Adds belongsToThrough relation to laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model models znck belongstothrough

23/07 2017

v2.3.1

2.3.1.0 https://github.com/znck/belongs-to-through

Adds belongsToThrough relation to laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model models znck belongstothrough

27/03 2017

v2.3.0

2.3.0.0 https://github.com/znck/belongs-to-through

Adds belongsToThrough relation to laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model models znck belongstothrough

29/05 2016

v2.2.2

2.2.2.0 https://github.com/znck/belongs-to-through

Adds belongsToThrough relation to laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model models znck belongstothrough

07/04 2016

v2.2.1

2.2.1.0 https://github.com/znck/belongs-to-through

Adds belongsToThrough relation to laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model models znck belongstothrough

03/02 2016

v2.2

2.2.0.0 https://github.com/znck/belongs-to-through

Adds belongsToThrough relation to laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model models znck belongstothrough

10/12 2015

v2.1

2.1.0.0 https://github.com/znck/belongs-to-through

Adds belongsToThrough relation to laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model models znck belongstothrough

17/11 2015

v2.0

2.0.0.0 https://github.com/znck/belongs-to-through

Adds belongsToThrough relation to laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model models znck belongstothrough

23/10 2015

v2.0-alpha

2.0.0.0-alpha https://github.com/znck/belongs-to-through

Adds belongsToThrough relation to laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model models znck belongstothrough

15/06 2015

v1.0.1

1.0.1.0 https://github.com/znck/belongs-to-through

Adds belongsToThrough relation to laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model models znck belongstothrough

06/06 2015

v1.0

1.0.0.0 https://github.com/znck/belongs-to-through

Adds belongsToThrough relation to laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model models znck belongstothrough