|
1 | 1 | # Relations |
2 | 2 |
|
3 | | -- [One-to-one](#one-to-one) |
4 | | -- [One-to-many](#one-to-many) |
5 | | -- [One-to-many inverse](#one-to-many-inverse) |
6 | | -- [One-of-many](#one-of-many) |
| 3 | +- [One to one](#one-to-one) |
| 4 | +- [One to many](#one-to-many) |
| 5 | +- [One to many inverse](#one-to-many-inverse) |
| 6 | +- [One of many](#one-of-many) |
| 7 | +- [Has one through](#has-one-through) |
| 8 | +- [Has many through](#has-many-through) |
7 | 9 |
|
8 | | -## One-to-one |
| 10 | +## One to one |
9 | 11 |
|
10 | 12 | A one-to-one relationship where one model is associated with exactly one instance of another model. |
11 | 13 |
|
@@ -38,7 +40,7 @@ class UserModel extends Model |
38 | 40 | model(UserModel::class)->with('profile')->find(1); |
39 | 41 | ``` |
40 | 42 |
|
41 | | -## One-to-many |
| 43 | +## One to many |
42 | 44 |
|
43 | 45 | A one-to-many relationship where one model is associated with multiple instances of another model. |
44 | 46 |
|
@@ -71,7 +73,7 @@ class UserModel extends Model |
71 | 73 | model(UserModel::class)->with('posts')->find(1); |
72 | 74 | ``` |
73 | 75 |
|
74 | | -## One-to-many inverse |
| 76 | +## One to many inverse |
75 | 77 |
|
76 | 78 | A one-to-many inverse relationship where a model belongs to another model. |
77 | 79 |
|
@@ -104,7 +106,7 @@ class PostModel extends Model |
104 | 106 | model(PostModel::class)->with('user')->find(1); |
105 | 107 | ``` |
106 | 108 |
|
107 | | -## One-of-many |
| 109 | +## One of many |
108 | 110 |
|
109 | 111 | A specialized type of one-to-one relationship where a parent model has multiple related records, but only one of them is considered active or relevant at any given time, based on a specific condition (e.g., the most recent, the highest priority, or the one meeting a custom criterion). |
110 | 112 |
|
@@ -157,3 +159,73 @@ model(UserModel::class)->with('firstPost')->find(1); |
157 | 159 |
|
158 | 160 | model(UserModel::class)->with('bestPost')->find(1); |
159 | 161 | ``` |
| 162 | + |
| 163 | +## Has one through |
| 164 | + |
| 165 | +A one-to-one relationship that is linked through an intermediate model. This means the parent model is related to a single record in the final model through another model acting as a bridge. |
| 166 | + |
| 167 | +### Example |
| 168 | + |
| 169 | +Consider an application where: |
| 170 | + |
| 171 | +- A **User** belongs to a **Company**. |
| 172 | +- Each **Company** has an **Address**. |
| 173 | + |
| 174 | +You want to retrieve a User's Address without needing to manually query through the Company. |
| 175 | + |
| 176 | +```php |
| 177 | +class UserModel extends Model |
| 178 | +{ |
| 179 | + use HasRelations; |
| 180 | + |
| 181 | + // ... |
| 182 | + |
| 183 | + public function initialize() |
| 184 | + { |
| 185 | + $this->initRelations(); |
| 186 | + } |
| 187 | + |
| 188 | + public function address(): Relation |
| 189 | + { |
| 190 | + return $this->hasOneThrough(AddressModel::class, CompanyModel::class); |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +### Usage |
| 196 | + |
| 197 | +```php |
| 198 | +model(UserModel::class)->with('address')->find(1); |
| 199 | +``` |
| 200 | + |
| 201 | +## Has many through |
| 202 | + |
| 203 | +A one-to-many relationship that is linked through an intermediate model. This means the parent model is related to multiple records in the final model through another model acting as a bridge. |
| 204 | + |
| 205 | +### Example |
| 206 | + |
| 207 | +Consider an application where:: |
| 208 | + |
| 209 | +- A *Country* has many *Users*. |
| 210 | +- A *User* has many *Posts*. |
| 211 | + |
| 212 | +You want to fetch all Posts for a Country, even though the Posts table does not directly reference the Country. |
| 213 | + |
| 214 | +```php |
| 215 | +class CountryModel extends Model |
| 216 | +{ |
| 217 | + use HasRelations; |
| 218 | + |
| 219 | + // ... |
| 220 | + |
| 221 | + public function initialize() |
| 222 | + { |
| 223 | + $this->initRelations(); |
| 224 | + } |
| 225 | + |
| 226 | + public function posts(): Relation |
| 227 | + { |
| 228 | + return $this->hasManyThrough(PostModel::class, UserModel::class); |
| 229 | + } |
| 230 | +} |
| 231 | +``` |
0 commit comments