Many To Many Morph
So let’s say we have a tag system.A video or post can have many tags,a tag can belong to many videos/posts. That’s a
typical many to many morph relation. In order to achieve that, we need a pivot table.Pivot table tagable_id bounds to
video/post.id,pivot table tagable_type indicate which type (post/video), pivot table tag_id bounds to tag.id.
Table Structure
post |
Model Structure
type Tag struct { |
MorphToMany
takes 8 parameter
- first one is a pointer of current model
- second is a pointer of related model
- third is pivottable
- 4th is pivottable relatedModelKey
- 5th is pivotTable currentTable key
- 6th is current model key
- 7th is related model key
- 8th is pivottable relatedType
MorphByMany
takes 8 parameter
- first one is a pointer of current model
- second is a pointer of related model
- third is pivottable
- 4th is pivottable currentModelKey
- 5th is pivotTable relatedModelkey
- 6th is current model key
- 7th is related model key
- 8th is pivottable relatedType
Usage Example
Use With when retrive
|