Polymorphism in the database

This page summarizes the projects mentioned and recommended in the original post on /r/laravel

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • laravel-medialibrary

    Associate files with Eloquent models

  • An example of a good use of a polymorphic relationship can be seen in the Rails Active Storage module, which allows you to attach files to a model, and instead of duplicating the database columns and tables for each model that has attachments, there are two general tables: activestorage_attachments (attachments; relationship info) and activestorage_blobs (blobs; file info). A model can have one or many attachments (has_one_attachment and has_many_attachments, both can be used more than once depending on your needs), an attachment belongs to a record (record_id and record_type) and belongs to a blob (blob_id), and blobs have one attachment. You can access an attachment attribute on the related model like so, user.avatar # has_one_attachment(:avatar); or for many attachments of the same type, user.photos # an array of attachments; has_many_attachments(:photos). There’s a Laravel package similar to this called spatie/laravel-medialibrary.

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts