You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: eloquent-relationships.md
+40Lines changed: 40 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -411,6 +411,16 @@ Now that we have examined the table structure for the relationship, let's define
411
411
412
412
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.
413
413
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
+
414
424
<aname="has-one-through-key-conventions"></a>
415
425
#### Key Conventions
416
426
@@ -434,6 +444,16 @@ Typical Eloquent foreign key conventions will be used when performing the relati
434
444
}
435
445
}
436
446
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
+
437
457
<aname="has-many-through"></a>
438
458
### Has Many Through
439
459
@@ -474,6 +494,16 @@ Now that we have examined the table structure for the relationship, let's define
474
494
475
495
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.
476
496
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:
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.
478
508
479
509
<aname="has-many-through-key-conventions"></a>
@@ -496,6 +526,16 @@ Typical Eloquent foreign key conventions will be used when performing the relati
496
526
}
497
527
}
498
528
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:
0 commit comments