### Installing BelongsToThrough Package with Composer Source: https://github.com/staudenmeir/belongs-to-through/blob/main/README.md This command installs the `staudenmeir/belongs-to-through` package using Composer, specifying version `^2.5`. This is the standard way to add the package to a Laravel project. ```Shell composer require staudenmeir/belongs-to-through:"^2.5" ``` -------------------------------- ### Installing BelongsToThrough Package in PowerShell Source: https://github.com/staudenmeir/belongs-to-through/blob/main/README.md This command is an alternative for installing the `staudenmeir/belongs-to-through` package via Composer when using PowerShell on Windows, escaping the version string differently due to PowerShell's parsing rules. ```Shell composer require staudenmeir/belongs-to-through:"^^^^2.5" ``` -------------------------------- ### Defining BelongsToThrough with Multiple Intermediates Source: https://github.com/staudenmeir/belongs-to-through/blob/main/README.md This PHP example shows how to define a `belongsToThrough` relationship with multiple intermediate models. The `Comment` model is linked to `Country` through `User` and `Post` by supplying an array of intermediate models as the second argument. ```PHP 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]); } } ``` -------------------------------- ### Defining BelongsToThrough with Table Aliases Source: https://github.com/staudenmeir/belongs-to-through/blob/main/README.md This PHP example shows how to use table aliases in a `belongsToThrough` relationship when the same model appears multiple times in the relationship path. The alias is specified in the intermediate models array, and the `foreignKeyLookup` is used for the aliased model. ```PHP 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'] ); } } ``` -------------------------------- ### Including Soft-Deleted Intermediate Models with BelongsToThrough (PHP) Source: https://github.com/staudenmeir/belongs-to-through/blob/main/README.md This PHP snippet demonstrates how to include soft-deleted intermediate models in a `belongsToThrough` relationship using the `withTrashed()` method. It shows a `Comment` model defining a `country` relationship that traverses `User` and `Post` models, explicitly including soft-deleted `User` records by specifying the `users.deleted_at` column. It also illustrates the `User` model utilizing the `SoftDeletes` trait. ```php 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; } ``` -------------------------------- ### Defining BelongsToThrough with Custom Foreign Keys Source: https://github.com/staudenmeir/belongs-to-through/blob/main/README.md This PHP code demonstrates how to specify custom foreign keys for intermediate models in a `belongsToThrough` relationship. The `foreignKeyLookup` array maps a model class to its custom foreign key, allowing flexible database schema integration. ```PHP 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'] ); } } ``` -------------------------------- ### Defining BelongsToThrough with Custom Local Keys Source: https://github.com/staudenmeir/belongs-to-through/blob/main/README.md This PHP snippet illustrates how to define a `belongsToThrough` relationship using custom local keys. The `localKeyLookup` array specifies the local key for the intermediate model, enabling relationships where the default key names are not used. ```PHP 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'], ); } } ``` -------------------------------- ### Defining a Basic BelongsToThrough Relationship Source: https://github.com/staudenmeir/belongs-to-through/blob/main/README.md This PHP snippet demonstrates how to define a basic `belongsToThrough` relationship in a `Post` model. It uses the `BelongsToThrough` trait to link `Post` to `Country` through the `User` model, establishing an inverse `HasManyThrough` relationship. ```PHP class Post extends Model { use \Znck\Eloquent\Traits\BelongsToThrough; public function country(): \Znck\Eloquent\Relations\BelongsToThrough { return $this->belongsToThrough(Country::class, User::class); } } ``` -------------------------------- ### Defining a HasManyThrough Relationship in Laravel Source: https://github.com/staudenmeir/belongs-to-through/blob/main/README.md This PHP code defines a standard `hasManyThrough` relationship in a `Country` model, linking `Country` to `Post` through the `User` model. It serves as a contrast to the `BelongsToThrough` relationship. ```PHP class Country extends Model { public function posts() { return $this->hasManyThrough(Post::class, User::class); } } ``` -------------------------------- ### Applying HasTableAlias Trait to a Model Source: https://github.com/staudenmeir/belongs-to-through/blob/main/README.md This PHP snippet demonstrates the usage of the `HasTableAlias` trait. This trait must be applied to any model that is being aliased within a `BelongsToThrough` relationship, enabling Laravel to correctly handle the aliased table. ```PHP class Comment extends Model { use \Znck\Eloquent\Traits\HasTableAlias; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.