Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Code Examples of polymorphic relationships

Discover how to use Laravel's polymorphic relationships method in real-world applications. Browse 13 examples from popular open-source projects.

13 examples
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.

BookStackApp/BookStack

  • 17759
  • Created Aug 2015
  • Updated Nov 2025

Files in this example

  • app/Providers/AppServiceProvider.php
View full example

serversideup/financial-freedom

  • 2748
  • Created Mar 2021
  • Updated Nov 2025

Files in this example

  • app/Models/Transactions/Transaction.php
    • app/Services/Transactions/AddTransaction.php
View full example

laravelio/laravel.io

  • 2497
  • Created May 2016
  • Updated Nov 2025

Files in this 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
View full example

guillaumebriday/laravel-blog

  • 1797
  • Created Dec 2016
  • Updated Oct 2024

Files in this 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
View full example

pelican-dev/panel

  • 1722
  • Created Mar 2024
  • Updated Nov 2025

Files in this example

  • database/migrations/2025_05_01_193002_move_to_mountables.php
    • app/Models/Mount.php
      • app/Providers/AppServiceProvider.php
        • app/Models/Server.php
View full example

beromir/Servas

  • 739
  • Created May 2022
  • Updated Nov 2025

Files in this example

  • app/Models/Group.php
    • app/Models/Link.php
      • app/Http/Controllers/LinkController.php
View full example

Azuriom/Azuriom

  • 703
  • Created Jan 2020
  • Updated Oct 2025

Files in this example

  • database/migrations/2020_06_30_000000_create_attachments_table.php
    • app/Models/Attachment.php
      • app/Models/Traits/Attachable.php
        • app/Http/Controllers/Admin/PageAttachmentController.php
          • app/Models/Page.php
            • app/Models/Post.php
View full example

nafiesl/free-pmo

  • 457
  • Created Nov 2017
  • Updated Oct 2025

Files in this example

  • app/Entities/Projects/Project.php
    • app/Entities/Projects/Issue.php
      • app/Http/Controllers/Projects/CommentsController.php
View full example

academico-sis/academico

  • 338
  • Created Dec 2018
  • Updated Nov 2025

Files in this 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
View full example

tighten/onramp

  • 164
  • Created Jul 2019
  • Updated Nov 2025

Files in this example

  • app/Models/Module.php
    • app/Models/User.php
      • app/Models/Completion.php
View full example

MdMostaFizurRahaman/ecommerce

  • 29
  • Created May 2022
  • Updated Sep 2022

Files in this 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
View full example

idanieldrew/redact

  • 19
  • Created Feb 2022
  • Updated Oct 2023

Files in this example

  • Modules/Media/Models/Media.php
    • Modules/Media/Database/Migrations/2022_04_28_092229_create_media_table.php
      • Modules/Category/Models/Category.php
View full example

JuanDMeGon/Laravel-from-Scratch

  • 17
  • Created Apr 2020
  • Updated Mar 2021

Files in this example

  • app/Cart.php
    • app/Product.php
      • app/Http/Controllers/OrderController.php
View full example

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.