Skip to content

Commit 5be022a

Browse files
committed
document has many through improvements
1 parent b05a279 commit 5be022a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

eloquent-relationships.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,16 @@ Now that we have examined the table structure for the relationship, let's define
411411

412412
The first argument passed to the `hasOneThrough` method is the name of the final model we wish to access, while the second argument is the name of the intermediate model.
413413

414+
Or, if the relevant relationships have already been defined on all of the models involved in the relationship, you may fluently define a "has-one-through" relationship by invoking the `through` method and supplying the names of those relationships. For example, if the `Mechanic` model has a `cars` relationship and the `Car` model has an `owner` relationship, you may define a "has-one-through" relationship connecting the mechanic and the owner like so:
415+
416+
```php
417+
// String based syntax...
418+
return $this->through('cars')->has('owner');
419+
420+
// Dynamic syntax...
421+
return $this->throughCars()->hasOwner();
422+
```
423+
414424
<a name="has-one-through-key-conventions"></a>
415425
#### Key Conventions
416426

@@ -434,6 +444,16 @@ Typical Eloquent foreign key conventions will be used when performing the relati
434444
}
435445
}
436446

447+
Or, as discussed earlier, if the relevant relationships have already been defined on all of the models involved in the relationship, you may fluently define a "has-one-through" relationship by invoking the `through` method and supplying the names of those relationships. This approach offers the advantage of reusing the key conventions already defined on the existing relationships:
448+
449+
```php
450+
// String based syntax...
451+
return $this->through('cars')->has('owner');
452+
453+
// Dynamic syntax...
454+
return $this->throughCars()->hasOwner();
455+
```
456+
437457
<a name="has-many-through"></a>
438458
### Has Many Through
439459

@@ -474,6 +494,16 @@ Now that we have examined the table structure for the relationship, let's define
474494

475495
The first argument passed to the `hasManyThrough` method is the name of the final model we wish to access, while the second argument is the name of the intermediate model.
476496

497+
Or, if the relevant relationships have already been defined on all of the models involved in the relationship, you may fluently define a "has-many-through" relationship by invoking the `through` method and supplying the names of those relationships. For example, if the `Project` model has a `environments` relationship and the `Environment` model has a `deployments` relationship, you may define a "has-many-through" relationship connecting the project and the deployments like so:
498+
499+
```php
500+
// String based syntax...
501+
return $this->through('environments')->has('deployments');
502+
503+
// Dynamic syntax...
504+
return $this->throughEnvironments()->hasDeployments();
505+
```
506+
477507
Though the `Deployment` model's table does not contain a `project_id` column, the `hasManyThrough` relation provides access to a project's deployments via `$project->deployments`. To retrieve these models, Eloquent inspects the `project_id` column on the intermediate `Environment` model's table. After finding the relevant environment IDs, they are used to query the `Deployment` model's table.
478508

479509
<a name="has-many-through-key-conventions"></a>
@@ -496,6 +526,16 @@ Typical Eloquent foreign key conventions will be used when performing the relati
496526
}
497527
}
498528

529+
Or, as discussed earlier, if the relevant relationships have already been defined on all of the models involved in the relationship, you may fluently define a "has-many-through" relationship by invoking the `through` method and supplying the names of those relationships. This approach offers the advantage of reusing the key conventions already defined on the existing relationships:
530+
531+
```php
532+
// String based syntax...
533+
return $this->through('environments')->has('deployments');
534+
535+
// Dynamic syntax...
536+
return $this->throughEnvironments()->hasDeployments();
537+
```
538+
499539
<a name="many-to-many"></a>
500540
## Many To Many Relationships
501541

0 commit comments

Comments
 (0)