staudenmeir / eloquent-has-many-deep
Laravel Eloquent HasManyThrough relationships with unlimited levels
AI Architecture Analysis
This repository is indexed by RepoMind. By analyzing staudenmeir/eloquent-has-many-deep in our AI interface, you can instantly generate complete architecture diagrams, visualize control flows, and perform automated security audits across the entire codebase.
Our Agentic Context Augmented Generation (Agentic CAG) engine loads full source files into context on-demand, avoiding the fragmentation of traditional RAG systems. Ask questions about the architecture, dependencies, or specific features to see it in action.
Repository Overview (README excerpt)
Crawler viewEloquent HasManyDeep This extended version of allows relationships with unlimited intermediate models. It supports many-to-many and polymorphic relationships and all their possible combinations. It also supports some third-party packages. Supports Laravel 5.5+. Installation composer require staudenmeir/eloquent-has-many-deep:"^1.7" Use this command if you are in PowerShell on Windows (e.g. in VS Code): composer require staudenmeir/eloquent-has-many-deep:"^^^^1.7" Versions | Laravel | Package | |:--------|:--------| | 13.x | 1.22 | | 12.x | 1.21 | | 11.x | 1.20 | | 10.x | 1.18 | | 9.x | 1.17 | | 8.x | 1.14 | | 7.x | 1.12 | | 6.x | 1.11 | | 5.8 | 1.8 | | 5.5–5.7 | 1.7 | Usage The package offers two ways of defining deep relationships: You can concatenate existing relationships or specify the intermediate models, foreign and local keys manually. • Concatenating Existing Relationships • Constraints • Third-Party Packages • Defining Relationships Manually • HasMany • ManyToMany • MorphMany • MorphToMany • MorphedByMany • BelongsTo • HasOneDeep • Composite Keys • Intermediate and Pivot Data • Intermediate and Pivot Constraints • Table Aliases • Soft Deleting • Getting Unique Results • Reversing Relationships • IDE Helper Concatenating Existing Relationships Consider this example from the Laravel documentation with an additional level: → has many → → has many → → has many → You can define a relationship by concatenating existing relationships: Define a relationship with if you only want to retrieve a single related instance. Constraints By default, constraints from the concatenated relationships are not transferred to the new deep relationship. Use with the relationships as callable arrays to apply these constraints: Make sure to qualify the constraints' column names if they appear in multiple tables: instead of Third-Party Packages Besides native Laravel relationships, you can also concatenate relationships from these third-party packages: • https://github.com/korridor/laravel-has-many-merged: • https://github.com/staudenmeir/eloquent-json-relations: , , • https://github.com/staudenmeir/laravel-adjacency-list: Tree & Graph relationships • https://github.com/topclaudy/compoships: , , Defining Relationships Manually If you don't have all the necessary existing relationships to concatenate them, you can also define a deep relationship manually by specifying the intermediate models, foreign and local keys. HasMany Consider this example from the Laravel documentation with an additional level: → has many → → has many → → has many → Just like with , the first argument of is the related model. The second argument is an array of intermediate models, from the far parent (the model where the relationship is defined) to the related model. By default, uses the Eloquent conventions for foreign and local keys. You can also specify custom foreign keys as the third argument and custom local keys as the fourth argument: You can use placeholders for default keys: ManyToMany You can include relationships in the intermediate path. ManyToMany → HasMany Consider this example from the Laravel documentation with an additional level: → many to many → → has many → Add the pivot tables to the intermediate models: If you specify custom keys, remember to swap the foreign and local key on the "right" side of the pivot table: ManyToMany → ManyToMany Consider this example from the Laravel documentation with an additional level: → many to many → → many to many → Add the pivot table to the intermediate models: MorphMany You can include relationships in the intermediate path. Consider this example from the Laravel documentation with an additional level: → has many → → morph many → Specify the polymorphic foreign keys as an array, starting with the column: MorphToMany You can include relationships in the intermediate path. Consider this example from the Laravel documentation with an additional level: → has many → → morph to many → Add the pivot table to the intermediate models and specify the polymorphic foreign keys as an array, starting with the column: Remember to swap the foreign and local key on the "right" side of the pivot table: MorphedByMany You can include relationships in the intermediate path. Consider this example from the Laravel documentation with an additional level: → morphed by many → → has many → Add the pivot table to the intermediate models and specify the polymorphic local keys as an array, starting with the column: BelongsTo You can include relationships in the intermediate path: → morphed by many → → belongs to → Swap the foreign and local key: HasOneDeep Define a relationship if you only want to retrieve a single related instance: Composite Keys If multiple columns need to match between two tables, you can define a composite key with the class. Consider this example from the documentation with an additional level: → has many (matching & ) → → belongs to → Intermediate and Pivot Data Use to retrieve attributes from intermediate tables: By default, this will retrieve all the table's columns. Be aware that this executes a separate query to get the list of columns. You can specify the selected columns as the second argument: As the third argument, you can specify a custom accessor: If you retrieve data from multiple tables, you can use nested accessors: Use for the pivot tables of and / relationships: You can specify a custom pivot model as the third argument and a custom accessor as the fourth: Intermediate and Pivot Constraints You can apply constraints on intermediate and pivot tables: Table Aliases If your relationship path contains the same model multiple times, you can specify a table alias: Use the trait in the models you are aliasing: For pivot tables, this requires custom models: Use to specify a table alias when concatenating existing relationships: Soft Deleting By default, soft-deleted intermediate models will b…