F
Filament5mo ago
BenRay

Best Way to Implement a 1-1 Polymorphic Relationship on a Table

I have a StockCode model that can morph to different types of stockcodeables such as PaperBag, MeshBag, etc, which each have their own additonal stockcode information. Each stockcodeable has their own form. The form will create a StockCode and it’s specific stockcodeable. What is the best way to go about: 1. Listing all StockCodes in one table, allowing you to filter by stockcodeable. 2. Allowing creation of any stockcodeable from the table and have it load that specific stockcodeable’s form. 3. Clicking on a StockCode in the table and have it bring you to that specific stockcodeable’s edit form. Additional: Does a 1-1 polymorphic relationship even make sense here? Maybe I need to approach this problem differently. Thanks.
4 Replies
awcodes
awcodes5mo ago
What are you asking? Theres no reason you can’t have all 3 as long as your relationship is set up correctly.
BenRay
BenRayOP5mo ago
The way I have it setup right now is that I have each stockcodeable as it’s own resource. I then have a StockCode table setup with header actions to take you to each stockcodeable’s create page. Then for each row in the stockcode table I have getUrl to route to the stockcodeable’s edit page (based on the stockcodeable_type field). Is this the right way to go about this or is there a more elegant solution? Like maybe instead of having each stockcodeable as a resource maybe I should just conditionally load different forms in the StockCode resource form based on stockcodeable_type. Just looking for a second opinion on what the best way to approach this is.
awcodes
awcodes5mo ago
Each stockcodeable has different fields based on the model right? So those should definitely be separate resources. A 1:1 pivot seems odd to me, since that is usually a many to many, but may be ok for your app needs. If it’s working though, i think you’re ok. Don’t focus on optimization unless you have to. 🙂
BenRay
BenRayOP5mo ago
Yes, each stockcodeable has different fields. I have to do this 1:1 pivot since I’m limited by the program that generates the stockcodes in the db (which I have no control over). It’s working with my current implementation though. I find I always hyper focus too much on optimization. Thanks.

Did you find this page helpful?