Download the PHP package staudenmeir/eloquent-has-many-deep without Composer
On this page you can find all versions of the php package staudenmeir/eloquent-has-many-deep. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download staudenmeir/eloquent-has-many-deep
More information about staudenmeir/eloquent-has-many-deep
Files in staudenmeir/eloquent-has-many-deep
Package eloquent-has-many-deep
Short Description Laravel Eloquent HasManyThrough relationships with unlimited levels
License MIT
Informations about the package eloquent-has-many-deep
Eloquent HasManyDeep
This extended version of HasManyThrough
allows relationships with unlimited intermediate models.
It supports 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 |
---|---|
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:
Country
→ has many → User
→ has many → Post
→ has many → Comment
You can define a HasManyDeep
relationship by concatenating existing relationships:
Define a HasOneDeep
relationship with hasOneDeepFromRelations()
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 hasManyDeepFromRelationsWithConstraints()
with the relationships as callable arrays to apply these constraints:
Make sure to qualify the constraints' column names if they appear in multiple tables:
->where('posts.published', true)
instead of ->where('published', true)
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:
HasManyMerged
- https://github.com/staudenmeir/eloquent-json-relations:
BelongsToJson
,HasManyJson
,HasManyThroughJson
- https://github.com/staudenmeir/laravel-adjacency-list: Tree & Graph relationships
- https://github.com/topclaudy/compoships:
BelongsTo
,HasMany
,HasOne
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:
Country
→ has many → User
→ has many → Post
→ has many → Comment
Just like with hasManyThrough()
, the first argument of hasManyDeep()
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, hasManyDeep()
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 null
placeholders for default keys:
ManyToMany
You can include ManyToMany
relationships in the intermediate path.
ManyToMany → HasMany
Consider this example from the Laravel documentation
with an additional HasMany
level:
User
→ many to many → Role
→ has many → Permission
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 ManyToMany
level:
User
→ many to many → Role
→ many to many → Permission
Add the pivot table to the intermediate models:
MorphMany
You can include MorphMany
relationships in the intermediate path.
Consider this example from the Laravel
documentation with an additional level:
User
→ has many → Post
→ morph many → Comment
Specify the polymorphic foreign keys as an array, starting with the *_type
column:
MorphToMany
You can include MorphToMany
relationships in the intermediate path.
Consider this example from the
Laravel documentation with an additional level:
User
→ has many → Post
→ morph to many → Tag
Add the pivot table to the intermediate models and specify the polymorphic foreign keys as an array, starting with
the *_type
column:
Remember to swap the foreign and local key on the "right" side of the pivot table:
MorphedByMany
You can include MorphedByMany
relationships in the intermediate path.
Consider this example from the
Laravel documentation with an additional level:
Tag
→ morphed by many → Post
→ has many → Comment
Add the pivot table to the intermediate models and specify the polymorphic local keys as an array, starting with
the *_type
column:
BelongsTo
You can include BelongsTo
relationships in the intermediate path:
Tag
→ morphed by many → Post
→ belongs to → User
Swap the foreign and local key:
HasOneDeep
Define a HasOneDeep
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 CompositeKey
class.
Consider this example from the compoships
documentation with an
additional level:
User
→ has many (matching team_id
& category_id
) → Task
→ belongs to → Project
Intermediate and Pivot Data
Use withIntermediate()
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 withPivot()
for the pivot tables of BelongsToMany
and MorphToMany
/MorphedByMany
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 HasTableAlias
trait in the models you are aliasing:
For pivot tables, this requires custom models:
Use setAlias()
to specify a table alias when concatenating existing relationships:
Soft Deleting
By default, soft-deleted intermediate models will be excluded from the result. Use withTrashed()
to include them:
Getting Unique Results
Deep relationships with many-to-many segments can contain duplicate models in their results. If you want to get unique results, you can remove duplicates from the result collection:
If you need to remove duplicates in the query (e.g. for pagination), try adding distinct()
:
distinct()
doesn't work for all cases. If it doesn't work for you, use groupBy()
instead:
Reversing Relationships
You can define a HasManyDeep
/HasOneDeep
relationship by reversing an existing deep relationship
using hasManyDeepFromReverse()
/hasOneDeepFromReverse()
:
IDE Helper
If you are using barryvdh/laravel-ide-helper, this package provides a model hook that will correctly add relations when generating the type hints. The model hook is enabled by default using Package Discovery.
To enable it manually, add model hook to the model_hooks array.
To disable the model hook you have 3 options:
- Disable using .env
- Disable using config
- Disable by option out of Package Discovery
Disable using .env
Update your .env
file to include:
Disable using config
Publish the config and disable the setting directly:
Disable by opting out of Package Discovery
Update your composer.json
with the following:
Contributing
Please see CODE OF CONDUCT for details.
All versions of eloquent-has-many-deep with dependencies
illuminate/database Version ^11.0
staudenmeir/eloquent-has-many-deep-contracts Version ^1.2