Download the PHP package hamid09430/direct-nested-relation-object without Composer
On this page you can find all versions of the php package hamid09430/direct-nested-relation-object. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hamid09430/direct-nested-relation-object
More information about hamid09430/direct-nested-relation-object
Files in hamid09430/direct-nested-relation-object
Package direct-nested-relation-object
Short Description Laravel Eloquent HasManyThrough relationship with unlimited levels
License MIT
Informations about the package direct-nested-relation-object
Introduction
This extended version of HasManyThrough
allows relationships with unlimited intermediate models.
It supports polymorphic relationships and all their possible combinations.
Supports Laravel 5.5.29+.
Installation
composer require hamid09430/direct-nested-relation-object:1.1
Usage
- HasMany
- BelongsToMany
- MorphMany
- MorphToMany
- MorphedByMany
- BelongsTo
- Existing Relationships
- HasOneDeep
- Intermediate and Pivot Data
- Table Aliases
- Soft Deleting
HasMany
Using the documentation example 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 the default keys:
BelongsToMany
You can include BelongsToMany
relationships in the intermediate path.
Using the documentation example with an additional level:
User
→ belongs to many → Role
→ has many → Permission
Add the pivot table 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:
MorphMany
You can include MorphMany
relationships in the intermediate path.
Using the documentation example 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.
Using the documentation example 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.
Using the documentation example 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:
Existing Relationships
In complex cases, you can define a HasManyDeep
relationship by chaining existing relationships:
HasOneDeep
Use the HasOneDeep
relationship if you only want to retrieve a single related instance:
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:
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:
Soft Deleting
By default, soft-deleted intermediate models will be excluded from the result. Use withTrashed()
to include them: