Lucid - Implement a "Stat" that can be used in several Models



I want to create a Stat model that can be applied to several Models. This stats will apply to several Models: User, Company, Product. This Stat table will have the views, prints, etc. information of other models.

I’m a bit stuck here as Lucid doesn’t have Many to Many Polimorphic as Laravel. Any suggestion on how to implement this?



There is a third party package that provides polymorphic relationships, adonis-lucid-polymorphic. I’m not sure if it actually works, it didn’t seem to last time I looked at it (several weeks ago).

You could use an Exclusive Arc table, rather than a polymophic pivot. Not sure exactly how, or if, it works outside of postgres.

The easiest, and probably most performant, option is to just create several pivot tables & many:many relationships for each thing you’d otherwise polymorph.

So say you have categories, posts and videos, catgories & posts have a many:many relationship as for videos & categories.

You would create your categories, post and videos tables as normal, then create individual pivot tables; post_categories and video_categories

This introduces more tables into the DB, but it’s a much more performant join than having to match a foreign table name and an id as is necessary for polymorphic joins.