### Install Eloquent HasManyDeep (Composer) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Command to install the staudenmeir/eloquent-has-many-deep package using Composer for standard environments. ```Shell composer require staudenmeir/eloquent-has-many-deep:"^1.7" ``` -------------------------------- ### Install Eloquent HasManyDeep (PowerShell) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Command to install the staudenmeir/eloquent-has-many-deep package using Composer specifically for PowerShell on Windows, requiring extra care with quotes. ```Shell composer require staudenmeir/eloquent-has-many-deep:"^^^^1.7" ``` -------------------------------- ### Access Pivot Data with Custom Accessor (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Example demonstrating how to access pivot data when a custom accessor name has been specified using `withPivot()`. The data is accessed via the custom property name defined. ```php foreach ($user->permissions as $permission) { // $permission->pivot->expires_at } ``` -------------------------------- ### Access Multiple Intermediate Data with Nested Accessors (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Example showing how to access data retrieved from multiple intermediate tables using nested, dot-notation accessors. This structure reflects the relationship path and organization defined with `withIntermediate()`. ```php foreach ($country->comments as $comment) { // $comment->post->title // $comment->post->user->name } ``` -------------------------------- ### Access Retrieved Intermediate Data (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Example showing how to access the intermediate data retrieved using `withIntermediate()`. The data from the intermediate table (Post in this case) is available as a property on the related model instance ($comment). ```php foreach ($country->comments as $comment) { // $comment->post->title } ``` -------------------------------- ### Access Intermediate Data with Custom Accessor (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Example demonstrating how to access intermediate data when a custom accessor name has been specified using `withIntermediate()`. The data is accessed via the custom property name defined. ```php foreach ($country->comments as $comment) { // $comment->accessor->title } ``` -------------------------------- ### Get Unique Deep Relationship Results (Collection) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Retrieve results from a deep relationship and remove duplicates using the `unique()` method on the resulting collection. This is suitable when duplicates are expected and can be handled after fetching. ```php $uniqueComments = Country::find($id)->comments()->get()->unique(); ``` -------------------------------- ### Access Retrieved Pivot Data (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Example showing how to access the pivot data retrieved using `withPivot()`. The data from the pivot table is available under a default property name (the pivot table name) on the related model instance ($permission). ```php foreach ($user->permissions as $permission) { // $permission->role_user->expires_at } ``` -------------------------------- ### Get Unique Deep Relationship Results (Query - Distinct) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Attempt to remove duplicates directly in the database query using the `distinct()` method. Note that this method may not work correctly for all deep relationship configurations. ```php $uniqueComments = Country::find($id)->comments()->distinct()->get(); ``` -------------------------------- ### Get Unique Deep Relationship Results (Query - GroupBy) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Remove duplicates in the query by accessing the underlying query builder, selecting only columns from the related table, and grouping by the related table's primary key. This is a more robust method when `distinct()` fails. ```php $uniqueComments = Country::find($id)->comments() ->getQuery() // Get the underlying query builder ->select('comments.*') // Select only columns from the related table ->groupBy('comments.id') // Group by the related table's primary key ->get(); ``` -------------------------------- ### Configure IDE Helper Model Hook Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Manually enable the package's model hook in the `barryvdh/laravel-ide-helper` configuration file (`config/ide-helper.php`) to ensure correct type hints are generated for deep relationships. ```php // File: config/ide-helper.php /* |-------------------------------------------------------------------------- | Models hooks |-------------------------------------------------------------------------- | | Define which hook classes you want to run for models to add custom information | | Hooks should implement Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface. | */ 'model_hooks' => [ \Staudenmeir\EloquentHasManyDeep\IdeHelper\DeepRelationsHook::class, ], ``` -------------------------------- ### Retrieve All Intermediate Data (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Demonstrates how to use `withIntermediate()` on a `hasManyDeep` relationship to fetch all columns from a specified intermediate table. The fetched data is accessible as a property on the related model instances. ```php public function comments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Comment::class, [User::class, Post::class]) ->withIntermediate(Post::class); } ``` -------------------------------- ### Disable IDE Helper Hook via Package Discovery Opt-Out Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Prevent the IDE Helper model hook from being automatically registered by opting out of Package Discovery for the `staudenmeir/eloquent-has-many-deep` package in your `composer.json` file. ```json "extra": { "laravel": { "dont-discover": [ "staudenmeir/eloquent-has-many-deep" ] } }, ``` -------------------------------- ### Retrieve Pivot Data (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Demonstrates using `withPivot()` on a `hasManyDeep` relationship that traverses a `BelongsToMany` or `MorphToMany` relationship. It fetches specified columns from the pivot table, making them available on the related model instances. ```php public function permissions(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Permission::class, ['role_user', Role::class]) ->withPivot('role_user', ['expires_at']); } ``` -------------------------------- ### Disable IDE Helper Hook via Config File Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Disable the IDE Helper model hook by publishing the package's configuration file (`config/eloquent-has-many-deep.php`) and setting the `ide_helper_enabled` option to `false`. ```php // File: config/eloquent-has-many-deep.php /* |-------------------------------------------------------------------------- | IDE Helper |-------------------------------------------------------------------------- | | Automatically register the model hook to receive correct type hints | */ 'ide_helper_enabled' => false, ``` -------------------------------- ### Defining HasManyDeep Relationship with Default Keys (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Defines a deep `HasManyDeep` relationship from `Country` to `Comment` through `User` and `Post` models. It uses the default Eloquent foreign and local key conventions. Requires the `Staudenmeir\EloquentHasManyDeep\HasRelationships` trait. ```php class Country extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function comments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Comment::class, [User::class, Post::class]); } } ``` -------------------------------- ### Disable IDE Helper Hook via Environment Variable Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Disable the automatic registration of the IDE Helper model hook by setting the `ELOQUENT_HAS_MANY_DEEP_IDE_HELPER_ENABLED` environment variable to `false` in your `.env` file. ```dotenv ELOQUENT_HAS_MANY_DEEP_IDE_HELPER_ENABLED=false ``` -------------------------------- ### Retrieve Specific Intermediate Data Columns (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Shows how to limit the columns retrieved from an intermediate table by passing an array of column names as the second argument to `withIntermediate()`. This improves performance by selecting only necessary data. ```php public function comments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Comment::class, [User::class, Post::class]) ->withIntermediate(Post::class, ['id', 'title']); } ``` -------------------------------- ### Define HasManyDeep Relationship (Concatenating) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Demonstrates defining a deep relationship ('comments' on 'Country') by concatenating existing relationships ('posts' on 'Country' and 'comments' on 'Post') using the 'hasManyDeepFromRelations' method. ```PHP class Country extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function comments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeepFromRelations($this->posts(), (new Post())->comments()); } public function posts() { return $this->hasManyThrough(Post::class, User::class); } } class Post extends Model { public function comments() { return $this->hasMany(Comment::class); } } ``` -------------------------------- ### Define Reverse Deep Relationship Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Define a reverse deep relationship (`HasOneDeep` in this case) by referencing an existing deep relationship using the `hasOneDeepFromReverse()` method. ```php class Country extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function comments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Comment::class, [User::class, Post::class]); } } class Comment extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function country(): \Staudenmeir\EloquentHasManyDeep\HasOneDeep { return $this->hasOneDeepFromReverse( (new Country())->comments() ); } } ``` -------------------------------- ### Retrieve Pivot Data with Custom Model/Accessor (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Shows how to use `withPivot()` with a custom pivot model (third argument) and a custom accessor name (fourth argument). This is useful when the pivot table has a dedicated model or when a different property name is desired. ```php public function permissions(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Permission::class, ['role_user', Role::class]) ->withPivot('role_user', ['expires_at'], RoleUser::class, 'pivot'); } ``` -------------------------------- ### Defining HasManyDeep with ManyToMany Pivot Table (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Defines a deep `HasManyDeep` relationship from `User` to `Permission` that includes a Many-to-Many relationship through the `role_user` pivot table and the `Role` model. The pivot table name is included in the intermediate path array. Uses default Eloquent conventions for keys. Requires the `Staudenmeir\EloquentHasManyDeep\HasRelationships` trait. ```php class User extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function permissions(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Permission::class, ['role_user', Role::class]); } } ``` -------------------------------- ### Defining HasManyDeep Relationship with Custom Keys (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Defines a deep `HasManyDeep` relationship from `Country` to `Comment` through `User` and `Post` models, explicitly specifying custom foreign and local keys for each step in the relationship path. Requires the `Staudenmeir\EloquentHasManyDeep\HasRelationships` trait. ```php class Country extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function comments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep( Comment::class, [User::class, Post::class], // Intermediate models, beginning at the far parent (Country). [ 'country_id', // Foreign key on the "users" table. 'user_id', // Foreign key on the "posts" table. 'post_id' // Foreign key on the "comments" table. ], [ 'id', // Local key on the "countries" table. 'id', // Local key on the "users" table. 'id' // Local key on the "posts" table. ] ); } } ``` -------------------------------- ### Apply Constraints on Intermediate Tables (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Explains how to filter the results of a `hasManyDeep` relationship based on conditions in one of the intermediate tables. Constraints are applied using standard query builder methods on the relationship instance, referencing the intermediate table by name. ```php $commentsFromActiveUsers = $country->comments()->where('users.active', true)->get(); ``` -------------------------------- ### Defining HasManyDeep Relationship with Null Placeholders (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Defines a deep `HasManyDeep` relationship from `Country` to `Comment`, demonstrating the use of `null` placeholders in the custom foreign key array to use default Eloquent conventions for specific keys while overriding others. Requires the `Staudenmeir\EloquentHasManyDeep\HasRelationships` trait. ```php class Country extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function comments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Comment::class, [User::class, Post::class], [null, 'custom_user_id']); } } ``` -------------------------------- ### Defining HasManyDeep with ManyToMany Pivot Table and Custom Keys (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Defines a deep `HasManyDeep` relationship from `User` to `Permission` through a Many-to-Many pivot table (`role_user`) and the `Role` model, explicitly specifying custom foreign and local keys. Notes the requirement to swap foreign and local keys for the pivot table in the respective arrays. Requires the `Staudenmeir\EloquentHasManyDeep\HasRelationships` trait. ```php class User extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function permissions(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep( Permission::class, ['role_user', Role::class], // Intermediate models and tables, beginning at the far parent (User). [ 'user_id', // Foreign key on the "role_user" table. 'id', // Foreign key on the "roles" table (local key). 'role_id' // Foreign key on the "permissions" table. ], [ 'id', // Local key on the "users" table. 'role_id', // Local key on the "role_user" table (foreign key). 'id' // Local key on the "roles" table. ] ); } } ``` -------------------------------- ### Define HasManyDeep Relationship (With Constraints) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Shows how to define a deep relationship while transferring constraints from the concatenated relationships using 'hasManyDeepFromRelationsWithConstraints'. Requires passing relationships as callable arrays and qualifying column names in constraints. ```PHP class Country extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function comments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeepFromRelationsWithConstraints([$this, 'posts'], [new Post(), 'comments']); } public function posts() { return $this->hasManyThrough(Post::class, User::class)->where('posts.published', true); } } class Post extends Model { public function comments() { return $this->hasMany(Comment::class)->withTrashed(); } } ``` -------------------------------- ### Use Table Alias for Pivot Tables (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Shows how to apply a table alias to a pivot table within the `hasManyDeep` relationship path array. This requires defining a custom pivot model that uses the `HasTableAlias` trait. ```php class User extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function permissions(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Permission::class, [RoleUser::class . ' as alias', Role::class]); } } ``` -------------------------------- ### Define Deep Relationship with Soft Deletes Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Configure a `HasManyDeep` relationship to include soft-deleted intermediate models using the `withTrashed()` method, specifying the table and column of the soft-deleted timestamp. ```php class Country extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function comments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Comment::class, [User::class, Post::class]) ->withTrashed('users.deleted_at'); } } class User extends Model { use SoftDeletes; } ``` -------------------------------- ### Retrieve Multiple Intermediate Data with Nested Accessors (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Explains how to fetch data from multiple intermediate tables in a deep relationship by chaining `withIntermediate()` calls. Custom, dot-notation accessors can be used to organize the intermediate data hierarchically on the related model. ```php public function comments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Comment::class, [User::class, Post::class]) ->withIntermediate(Post::class) ->withIntermediate(User::class, ['*'], 'post.user'); } ``` -------------------------------- ### Define Relationship for Intermediate Constraints (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Basic model definition showing the use of the `HasRelationships` trait and a `hasManyDeep` relationship, which is a prerequisite for applying constraints on intermediate tables. ```php class Country extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function comments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Comment::class, [User::class, Post::class]); } } ``` -------------------------------- ### Defining BelongsTo Deep Relationship in Laravel PHP Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md This snippet defines a deep relationship from Tag to User through the 'taggables' pivot table and Post, where the relationship from Post to User is a BelongsTo relationship. It uses `hasManyDeep`, specifying intermediate tables/models and custom local/foreign keys, demonstrating how to handle the BelongsTo link by swapping keys. ```php class Tag extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function postAuthors(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep( User::class, ['taggables', Post::class], [null, 'id', 'id'], [null, ['taggable_type', 'taggable_id'], 'user_id'] ); } } ``` -------------------------------- ### Retrieve Intermediate Data with Custom Accessor (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Illustrates using the third argument of `withIntermediate()` to specify a custom property name (accessor) under which the intermediate table's data will be available on the related model. This avoids naming conflicts when retrieving data from multiple intermediate tables. ```php public function comments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Comment::class, [User::class, Post::class]) ->withIntermediate(Post::class, ['id', 'title'], 'accessor'); } ``` -------------------------------- ### Defining HasOneDeep Relationship in Laravel PHP Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md This snippet defines a deep relationship from Country to a single latest Comment through User and Post. It uses the `hasOneDeep` method, similar to `hasManyDeep`, but configured to return only one result, often combined with ordering like `latest()`. ```php class Country extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function latestComment(): \Staudenmeir\EloquentHasManyDeep\HasOneDeep { return $this->hasOneDeep(Comment::class, [User::class, Post::class]) ->latest('comments.created_at'); } } ``` -------------------------------- ### Use Table Alias in Relationship Path (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Demonstrates how to use a table alias when the same model appears multiple times in the `hasManyDeep` relationship path array. The alias is specified using 'ModelName as alias_name' syntax. The aliased model must use the `HasTableAlias` trait. ```php class Post extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function commentReplies(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Comment::class, [Comment::class . ' as alias'], [null, 'parent_id']); } } ``` -------------------------------- ### Defining Deep Relationship with Composite Keys in Laravel PHP Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md This snippet defines a deep relationship from User to Project through Task, where the relationship between User and Task uses a composite key (`team_id`, `category_id`). It utilizes the `CompositeKey` class within the `hasManyDeep` method to specify the multiple columns for matching. ```php use Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey; class User extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function projects(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep( Project::class, [Task::class], [new CompositeKey('team_id', 'category_id'), 'id'], [new CompositeKey('team_id', 'category_id'), 'project_id'] ); } } ``` -------------------------------- ### Apply HasTableAlias Trait to Aliased Pivot Model (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Shows that a custom pivot model used with a table alias in a `hasManyDeep` relationship path must include the `\Staudenmeir\EloquentHasManyDeep\HasTableAlias` trait. ```php class RoleUser extends Pivot { use \Staudenmeir\EloquentHasManyDeep\HasTableAlias; } ``` -------------------------------- ### Defining ManyToMany Deep Relationship in Laravel PHP Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md This snippet defines a deep ManyToMany relationship from User to Permission through Role and the pivot tables 'role_user' and 'permission_role'. It uses the `hasManyDeep` method provided by the `staudenmeir/eloquent-has-many-deep` package to traverse multiple intermediate tables. ```php class User extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function permissions(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep(Permission::class, ['role_user', Role::class, 'permission_role']); } } ``` -------------------------------- ### Apply Alias When Concatenating Relationships (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Illustrates using `setAlias()` on a relationship instance before passing it to `hasManyDeepFromRelations()`. This is necessary to apply an alias when building a deep relationship by combining existing relationships, especially when a model is used multiple times. ```php class Post extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function commentReplies(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeepFromRelations( $this->comments(), (new Comment())->setAlias('alias')->replies() ); } public function comments() { return $this->hasMany(Comment::class); } } ``` -------------------------------- ### Defining MorphMany Deep Relationship in Laravel PHP Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md This snippet defines a deep relationship from User to Comment through Post, where the relationship from Post to Comment is a MorphMany relationship. It uses the `hasManyDeep` method and specifies the polymorphic foreign keys (`commentable_type`, `commentable_id`) in the third argument. ```php class User extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function postComments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep( Comment::class, [Post::class], [null, ['commentable_type', 'commentable_id']] ); } } ``` -------------------------------- ### Define Relationship to be Aliased and Concatenated (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Defines a standard `hasMany` relationship that serves as the second base relationship to be concatenated using `hasManyDeepFromRelations()`. This relationship is the one that will have an alias applied via `setAlias()` before concatenation. ```php class Comment extends Model { use \Staudenmeir\EloquentHasManyDeep\HasTableAlias; public function replies() { return $this->hasMany(self::class, 'parent_id'); } } ``` -------------------------------- ### Defining MorphToMany Deep Relationship in Laravel PHP Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md This snippet defines a deep relationship from User to Tag through Post and the 'taggables' pivot table, where the relationship from Post to Tag is a MorphToMany relationship. It uses `hasManyDeep`, specifying the intermediate models/tables, polymorphic foreign keys (`taggable_type`, `taggable_id`), and custom local/foreign keys for the pivot. ```php class User extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function postTags(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep( Tag::class, [Post::class, 'taggables'], [null, ['taggable_type', 'taggable_id'], 'id'], [null, null, 'tag_id'] ); } } ``` -------------------------------- ### Apply HasTableAlias Trait to Aliased Model (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Shows that any model used with a table alias in a `hasManyDeep` relationship path must include the `\Staudenmeir\EloquentHasManyDeep\HasTableAlias` trait. ```php class Comment extends Model { use \Staudenmeir\EloquentHasManyDeep\HasTableAlias; } ``` -------------------------------- ### Define Base Relationship for Concatenation (PHP) Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md Defines a standard `hasMany` relationship that serves as one of the base relationships to be concatenated using `hasManyDeepFromRelations()`. This relationship is used in conjunction with another relationship that is aliased. ```php public function comments() { return $this->hasMany(Comment::class); } ``` -------------------------------- ### Defining MorphedByMany Deep Relationship in Laravel PHP Source: https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/README.md This snippet defines a deep relationship from Tag to Comment through the 'taggables' pivot table and Post, where the relationship from Tag to Post is a MorphedByMany relationship. It uses `hasManyDeep`, specifying the intermediate tables/models and custom local/foreign keys, including the polymorphic local keys (`taggable_type`, `taggable_id`). ```php class Tag extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function postComments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep { return $this->hasManyDeep( Comment::class, ['taggables', Post::class], [null, 'id'], [null, ['taggable_type', 'taggable_id']] ); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.