Examples of Polymorphic relationships (9)

Short information about polymorphic relationships

Polymorphic relationships are used when there may be multiple models related to one particular model, with the potential to have even more models in the future.
For example, Tags may be related to Articles, or to Photos, or maybe in the future to Users, or to Comments, etc. So, to avoid changing the database structure for those new models, you define a Polymorphic relationship, and re-use it for all current/future models, just by changing the Model classes code, but not the database itself.
Read more in the official Laravel docs.

Repository: BookStackApp/BookStack (Github)
★ 9804 Created: Aug 2015 Updated: Aug 2022 Laravel version: 8
Files in the example:
- app/Providers/AppServiceProvider.php
Repository: laravelio/laravel.io (Github)
★ 2186 Created: May 2016 Updated: Aug 2022 Laravel version: 8
Files in the example:
- app/Models/Tag.php
- app/Helpers/HasTags.php
- app/Models/Thread.php
- app/Helpers/HasLikes.php
- app/Models/Article.php
- app/Jobs/CreateArticle.php
- app/Jobs/LikeArticle.php
Repository: guillaumebriday/laravel-blog (Github)
★ 1503 Created: Dec 2016 Updated: Jun 2022 Laravel version: 8
Files in the example:
- database/migrations/2017_11_15_003340_create_likes_table.php
- app/Models/Like.php
- app/Models/Post.php
- app/Concern/Likeable.php
- app/Http/Controllers/Api/V1/PostLikeController.php
Repository: serversideup/financial-freedom (Github)
★ 420 Created: Mar 2021 Updated: Jul 2022 Laravel version: 8
Files in the example:
- app/Models/Transactions/Transaction.php
- app/Services/Transactions/AddTransaction.php
Repository: nafiesl/free-pmo (Github)
★ 362 Created: Nov 2017 Updated: Jul 2022 Laravel version: 8
Files in the example:
- app/Entities/Projects/Project.php
- app/Entities/Projects/Issue.php
- app/Http/Controllers/Projects/CommentsController.php
Repository: tighten/onramp (Github)
★ 145 Created: Jul 2019 Updated: Aug 2022 Laravel version: 8
Files in the example:
- app/Models/Module.php
- app/Models/User.php
- app/Models/Completion.php
Repository: academico-sis/academico (Github)
★ 126 Created: Dec 2018 Updated: Aug 2022 Laravel version: 8
Files in the example:
- database/migrations/2018_12_20_142237_create_comments_table.php
- app/Models/Comment.php
- app/Http/Controllers/CommentController.php
- resources/views/students/show.blade.php
- resources/js/components/StudentCommentComponent.vue
- app/Models/Student.php
Repository: MdMostaFizurRahaman/ecommerce (Github)
★ 17 Created: May 2022 Updated: May 2022 Laravel version: 8
Files in the example:
- database/migrations/2021_10_30_003131_create_addresses_table.php
- app/Models/Address.php
- app/Traits/InteractsWithAddress.php
- app/Traits/CreateCustomer.php
- app/Traits/InteractsWithOrder.php
Repository: JuanDMeGon/Laravel-from-Scratch (Github)
★ 11 Created: Apr 2020 Updated: Mar 2021 Laravel version: 8
Files in the example:
- app/Cart.php
- app/Product.php
- app/Http/Controllers/OrderController.php

Additional resources on polymorphic relationships: