Skip to content

Commit d3f1eff

Browse files
committed
docs - pivot table for many-to-many relation
1 parent bbbd8fb commit d3f1eff

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

docs/relations.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ A one-to-many relationship that is linked through an intermediate model. This me
219219

220220
Consider an application where::
221221

222-
- A *Country* has many *Users*.
223-
- A *User* has many *Posts*.
222+
- A **Country** has many **Users**.
223+
- A **User** has many **Posts**.
224224

225225
You want to fetch all Posts for a Country, even though the Posts table does not directly reference the Country.
226226

@@ -257,11 +257,15 @@ A `belongsToMany` relationship is used for many-to-many associations between two
257257

258258
Consider an application where:
259259

260-
- *Students* can enroll in multiple *Courses*.
261-
- A *Course* can have multiple *Students*.
260+
- **Students** can enroll in multiple **Courses**.
261+
- A **Course** can have multiple **Students**.
262262

263263
Since both Students and Courses can be related to each other in many ways, we use a pivot table to manage this association.
264264

265+
The pivot table should have a name derived from the name of both tables. In this example, it would be `course_student` - note the singular form of the words. The order of the words is alphabetical.
266+
267+
The table should contain a primary key (`id` - auto_increment) and two foreign keys for both of models: `course_id` and `student_id`.
268+
265269
```php
266270
class StudentModel extends Model
267271
{

0 commit comments

Comments
 (0)